-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
239 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"path" | ||
"strings" | ||
"time" | ||
|
||
"github.com/moolite/bot/internal/db" | ||
"github.com/moolite/bot/internal/telegram" | ||
) | ||
|
||
const ( | ||
FILE_DOWNLOAD_OK = iota | ||
FILE_DOWNLOAD_ERR | ||
) | ||
|
||
func fileDownloaderWorker(workerId int, filenames <-chan []string) { | ||
for file := range filenames { | ||
fileId := file[0] | ||
filename := file[1] | ||
|
||
if err := telegram.DownloadFileId(Cfg, fileId, filename); err != nil { | ||
slog.Error("File failed to download", "workerId", workerId, "fileId", fileId, "filename", filename, "err", err) | ||
} else { | ||
slog.Info("downloaded file", "workerId", workerId, "filename", filename) | ||
} | ||
} | ||
} | ||
|
||
func SyncFolder(folder string) error { | ||
ctx := context.Background() | ||
dbResults, err := db.SelectAllMedia(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
slog.Debug("synchronizing files", "files", len(dbResults), "folder", folder) | ||
|
||
fileList, err := os.ReadDir(folder) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var fileIds [][]string | ||
for _, res := range dbResults { | ||
skip := false | ||
for _, f := range fileList { | ||
skip = strings.Contains(f.Name(), res.Data) | ||
} | ||
|
||
if !skip { | ||
fileExtension := "jpg" | ||
if res.Kind != "photo" { | ||
fileExtension = "m4v" | ||
} | ||
filename := path.Join(folder, res.Data+"."+fileExtension) | ||
fileIds = append(fileIds, []string{res.Data, filename}) | ||
} | ||
} | ||
|
||
slog.Debug("files to download", "files", len(fileIds)) | ||
|
||
limiter := time.Tick(100 * time.Millisecond) | ||
jobs := make(chan []string, len(fileList)) | ||
for w := 1; w <= 5; w++ { | ||
go fileDownloaderWorker(w, jobs) | ||
} | ||
|
||
for _, job := range fileIds { | ||
jobs <- job | ||
<-limiter | ||
} | ||
close(jobs) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package telegram | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/moolite/bot/internal/config" | ||
"github.com/valyala/fastjson" | ||
) | ||
|
||
var ( | ||
tgBaseFileApi string = "https://api.telegram.org/file/bot" | ||
) | ||
|
||
func GetLink(cfg *config.Config, id string) (string, error) { | ||
body := map[string]string{ | ||
"file_id": id, | ||
} | ||
|
||
bodyJson, err := json.Marshal(body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
resp, err := apiRequest(cfg.Telegram.Token, "getFile", bodyJson) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
slog.Debug("api request result", "json", string(resp)) | ||
|
||
p, err := fastjson.ParseBytes(resp) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if ok := p.GetBool("ok"); ok != true { | ||
return "", fmt.Errorf("error while fetching result: %s", resp) | ||
} | ||
|
||
fileId := string(p.GetStringBytes("result", "file_path")) | ||
if fileId == "" { | ||
return "", fmt.Errorf("file_path not found in json") | ||
} | ||
|
||
return tgBaseFileApi + cfg.Telegram.Token + "/" + fileId, nil | ||
} | ||
|
||
func DownloadFileId(cfg *config.Config, id, filename string) error { | ||
uri, err := GetLink(cfg, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.Get(uri) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode == 404 { | ||
return fmt.Errorf("404 file not found.") | ||
} | ||
|
||
if resp.StatusCode > 201 { | ||
return fmt.Errorf("error downloading file. StatusCode %d", resp.StatusCode) | ||
} | ||
|
||
fd, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer fd.Close() | ||
|
||
if _, err := io.Copy(fd, resp.Body); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |