diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml new file mode 100644 index 0000000..75452fa --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -0,0 +1,68 @@ +name: Bug Report +description: Report a bug in Twitter/X Media Batch Downloader +title: "[Bug Report] " +labels: ["bug"] + +body: + - type: markdown + attributes: + value: | + > **WARNING: Issues that do not follow this template will be deleted without review.** + > + > **Please keep `[Bug Report]` in the issue title and only continue after it.** + + - type: textarea + id: problem + attributes: + label: Problem + placeholder: e.g. Fetching @username/media stops after a few items, or GIF conversion fails with no clear error. + validations: + required: true + + - type: input + id: target + attributes: + label: X/Twitter URL or Username + placeholder: e.g. https://x.com/username/media or @username + validations: + required: true + + - type: textarea + id: steps + attributes: + label: Steps to Reproduce + placeholder: | + 1. Open ... + 2. Enter ... + 3. Click ... + 4. See error ... + validations: + required: true + + - type: textarea + id: logs + attributes: + label: Error Message / Logs / Screenshot + placeholder: e.g. failed to extract timeline: parse_error ... + validations: + required: true + + - type: markdown + attributes: + value: "### Environment" + + - type: input + id: version + attributes: + label: Twitter/X Media Batch Downloader Version + placeholder: e.g. v4.4 + validations: + required: true + + - type: input + id: os + attributes: + label: OS + placeholder: e.g. Windows 11 23H2 + validations: + required: true diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..ec4bb38 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: false \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml new file mode 100644 index 0000000..f48e1a5 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -0,0 +1,34 @@ +name: Feature Request +description: Request a feature for Twitter/X Media Batch Downloader +title: "[Feature Request] " +labels: ["enhancement"] + +body: + - type: markdown + attributes: + value: | + > **WARNING: Issues that do not follow this template will be deleted without review.** + > + > **Please keep `[Feature Request]` in the issue title and only continue after it.** + + - type: textarea + id: description + attributes: + label: Description + placeholder: e.g. Add an option to rotate between multiple auth tokens or export filtered results directly from Saved Accounts. + validations: + required: true + + - type: textarea + id: use-case + attributes: + label: Why is this needed? + placeholder: e.g. I manage multiple accounts and want a faster way to retry only failed fetches without re-running the whole batch. + validations: + required: true + + - type: textarea + id: additional-context + attributes: + label: Additional Context + placeholder: e.g. Mockup, similar app behavior, workflow notes, or screenshots if needed. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4664703 --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ +# Wails Build +build/ +*.exe +*.dll +*.dylib +*.so + +# Wails Generated Files +frontend/wailsjs/ + +# Go +*.test +*.out +go.work +go.work.sum + +# Node / Frontend +node_modules/ +frontend/node_modules/ +frontend/dist/ +frontend/.vite/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +.pnpm-store/ +.npm +.yarn +*.tsbuildinfo + +# IDE / Editors +.vscode/ +.idea/ +*.swp +*.swo +*~ +.DS_Store + +# OS +Thumbs.db +desktop.ini + +# Environment +.env +.env.local +.env.*.local + +# Logs +*.log +logs/ + +# Temporary files +tmp/ +temp/ +*.tmp +*.bak +*.old + +*.bat diff --git a/app.go b/app.go index d4afc7a..212223c 100644 --- a/app.go +++ b/app.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "path/filepath" "twitterxmediabatchdownloader/backend" @@ -154,6 +155,18 @@ func (a *App) GetDefaults() map[string]string { } } +func (a *App) GetStoredAuthToken(slot string) (string, error) { + return backend.GetStoredAuthToken(slot) +} + +func (a *App) SetStoredAuthToken(slot, token string) error { + return backend.SetStoredAuthToken(slot, token) +} + +func (a *App) ClearStoredAuthToken(slot string) error { + return backend.ClearStoredAuthToken(slot) +} + func (a *App) IsExtractorInstalled() bool { return backend.IsExtractorInstalled() } @@ -162,6 +175,10 @@ func (a *App) DownloadExtractor() error { return backend.DownloadExtractor(nil) } +func (a *App) GetExtractorVersionStatus() backend.ExtractorVersionStatus { + return backend.GetExtractorVersionStatus() +} + func (a *App) Quit() { panic("quit") } @@ -192,6 +209,7 @@ type DownloadMediaWithMetadataRequest struct { type DownloadMediaResponse struct { Success bool `json:"success"` + Cancelled bool `json:"cancelled"` Downloaded int `json:"downloaded"` Skipped int `json:"skipped"` Failed int `json:"failed"` @@ -201,8 +219,9 @@ type DownloadMediaResponse struct { func (a *App) DownloadMedia(req DownloadMediaRequest) (DownloadMediaResponse, error) { if len(req.URLs) == 0 { return DownloadMediaResponse{ - Success: false, - Message: "No URLs provided", + Success: false, + Cancelled: false, + Message: "No URLs provided", }, fmt.Errorf("no URLs provided") } @@ -219,6 +238,7 @@ func (a *App) DownloadMedia(req DownloadMediaRequest) (DownloadMediaResponse, er if err != nil { return DownloadMediaResponse{ Success: false, + Cancelled: false, Downloaded: downloaded, Skipped: 0, Failed: failed, @@ -228,6 +248,7 @@ func (a *App) DownloadMedia(req DownloadMediaRequest) (DownloadMediaResponse, er return DownloadMediaResponse{ Success: true, + Cancelled: false, Downloaded: downloaded, Skipped: 0, Failed: failed, @@ -250,8 +271,9 @@ type DownloadItemStatus struct { func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (DownloadMediaResponse, error) { if len(req.Items) == 0 { return DownloadMediaResponse{ - Success: false, - Message: "No items provided", + Success: false, + Cancelled: false, + Message: "No items provided", }, fmt.Errorf("no items provided") } @@ -286,6 +308,10 @@ func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (D } a.downloadCtx, a.downloadCancel = context.WithCancel(context.Background()) + defer func() { + a.downloadCtx = nil + a.downloadCancel = nil + }() progressCallback := func(current, total int) { percent := 0 @@ -309,8 +335,20 @@ func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (D downloaded, skipped, failed, err := backend.DownloadMediaWithMetadataProgressAndStatus(items, outputDir, req.Username, progressCallback, itemStatusCallback, a.downloadCtx, req.Proxy) if err != nil { + if errors.Is(err, context.Canceled) { + return DownloadMediaResponse{ + Success: false, + Cancelled: true, + Downloaded: downloaded, + Skipped: skipped, + Failed: failed, + Message: "Download stopped", + }, nil + } + return DownloadMediaResponse{ Success: false, + Cancelled: false, Downloaded: downloaded, Skipped: skipped, Failed: failed, @@ -318,10 +356,9 @@ func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (D }, err } - a.downloadCancel = nil - return DownloadMediaResponse{ Success: true, + Cancelled: false, Downloaded: downloaded, Skipped: skipped, Failed: failed, @@ -358,6 +395,14 @@ func (a *App) GetAccountFromDB(id int64) (string, error) { return acc.ResponseJSON, nil } +func (a *App) GetSavedAccountFromDB(username, mediaType string) (string, error) { + acc, err := backend.GetAccountByUsernameAndMediaType(username, mediaType) + if err != nil { + return "", err + } + return acc.ResponseJSON, nil +} + func (a *App) DeleteAccountFromDB(id int64) error { return backend.DeleteAccount(id) } @@ -382,6 +427,10 @@ func (a *App) GetAllGroups() ([]map[string]string, error) { return backend.GetAllGroups() } +func (a *App) CheckFoldersExist(basePath string, usernames []string) map[string]bool { + return backend.CheckFoldersExist(basePath, usernames) +} + func (a *App) IsFFmpegInstalled() bool { return backend.IsFFmpegInstalled() } @@ -390,6 +439,10 @@ func (a *App) DownloadFFmpeg() error { return backend.DownloadFFmpeg(nil) } +func (a *App) GetFFmpegVersionStatus() backend.DependencyVersionStatus { + return backend.GetFFmpegVersionStatus() +} + func (a *App) IsExifToolInstalled() bool { return backend.IsExifToolInstalled() } @@ -398,6 +451,10 @@ func (a *App) DownloadExifTool() error { return backend.DownloadExifTool(nil) } +func (a *App) GetExifToolVersionStatus() backend.DependencyVersionStatus { + return backend.GetExifToolVersionStatus() +} + type ConvertGIFsRequest struct { FolderPath string `json:"folder_path"` Quality string `json:"quality"` diff --git a/backend/auth_token.go b/backend/auth_token.go new file mode 100644 index 0000000..1cce1bb --- /dev/null +++ b/backend/auth_token.go @@ -0,0 +1,70 @@ +package backend + +import ( + "errors" + "fmt" + "strings" + + "github.com/zalando/go-keyring" +) + +const authTokenKeyringService = "TwitterXMediaBatchDownloader" + +func normalizeAuthTokenSlot(slot string) (string, error) { + switch strings.ToLower(strings.TrimSpace(slot)) { + case "public": + return "public", nil + case "private": + return "private", nil + default: + return "", fmt.Errorf("invalid auth token slot: %q", slot) + } +} + +func authTokenKeyringUser(slot string) string { + return "auth-token-" + slot +} + +func GetStoredAuthToken(slot string) (string, error) { + normalizedSlot, err := normalizeAuthTokenSlot(slot) + if err != nil { + return "", err + } + + token, err := keyring.Get(authTokenKeyringService, authTokenKeyringUser(normalizedSlot)) + if errors.Is(err, keyring.ErrNotFound) { + return "", nil + } + if err != nil { + return "", err + } + + return token, nil +} + +func SetStoredAuthToken(slot, token string) error { + normalizedSlot, err := normalizeAuthTokenSlot(slot) + if err != nil { + return err + } + + if strings.TrimSpace(token) == "" { + return ClearStoredAuthToken(normalizedSlot) + } + + return keyring.Set(authTokenKeyringService, authTokenKeyringUser(normalizedSlot), token) +} + +func ClearStoredAuthToken(slot string) error { + normalizedSlot, err := normalizeAuthTokenSlot(slot) + if err != nil { + return err + } + + err = keyring.Delete(authTokenKeyringService, authTokenKeyringUser(normalizedSlot)) + if errors.Is(err, keyring.ErrNotFound) { + return nil + } + + return err +} diff --git a/backend/config.go b/backend/config.go index f6fc8f3..e28db0b 100644 --- a/backend/config.go +++ b/backend/config.go @@ -6,6 +6,8 @@ import ( "net/url" "os" "path/filepath" + "strings" + "sync/atomic" "time" ) @@ -20,14 +22,42 @@ func GetDefaultDownloadPath() string { return filepath.Join(homeDir, "Pictures") } -func GetProxyURL(customProxy string) (*url.URL, error) { +func parseProxyURLs(rawValue string, sourceName string) ([]*url.URL, error) { + if strings.TrimSpace(rawValue) == "" { + return nil, nil + } + + parts := strings.Split(rawValue, ",") + proxyURLs := make([]*url.URL, 0, len(parts)) + + for _, part := range parts { + trimmed := strings.TrimSpace(part) + if trimmed == "" { + continue + } + + proxyURL, err := url.Parse(trimmed) + if err != nil { + return nil, fmt.Errorf("invalid proxy URL in %s: %v", sourceName, err) + } + if proxyURL.Scheme == "" || proxyURL.Host == "" { + return nil, fmt.Errorf("invalid proxy URL in %s: %q", sourceName, trimmed) + } + + proxyURLs = append(proxyURLs, proxyURL) + } + + if len(proxyURLs) == 0 { + return nil, nil + } + + return proxyURLs, nil +} + +func GetProxyURLs(customProxy string) ([]*url.URL, error) { if customProxy != "" { - proxyURL, err := url.Parse(customProxy) - if err != nil { - return nil, fmt.Errorf("invalid custom proxy URL: %v", err) - } - return proxyURL, nil + return parseProxyURLs(customProxy, "custom proxy setting") } proxyEnv := os.Getenv("HTTPS_PROXY") @@ -42,24 +72,43 @@ func GetProxyURL(customProxy string) (*url.URL, error) { } if proxyEnv != "" { - proxyURL, err := url.Parse(proxyEnv) - if err != nil { - return nil, fmt.Errorf("invalid proxy URL from environment: %v", err) - } - return proxyURL, nil + return parseProxyURLs(proxyEnv, "proxy environment variable") } return nil, nil } +func GetProxyURL(customProxy string) (*url.URL, error) { + proxyURLs, err := GetProxyURLs(customProxy) + if err != nil || len(proxyURLs) == 0 { + return nil, err + } + return proxyURLs[0], nil +} + +func buildProxySelector(proxyURLs []*url.URL) func(*http.Request) (*url.URL, error) { + if len(proxyURLs) == 0 { + return nil + } + if len(proxyURLs) == 1 { + return http.ProxyURL(proxyURLs[0]) + } + + var requestCount uint64 + return func(*http.Request) (*url.URL, error) { + index := atomic.AddUint64(&requestCount, 1) - 1 + return proxyURLs[index%uint64(len(proxyURLs))], nil + } +} + func CreateHTTPClient(customProxy string, timeout time.Duration) (*http.Client, error) { - proxyURL, err := GetProxyURL(customProxy) + proxyURLs, err := GetProxyURLs(customProxy) if err != nil { return nil, err } transport := &http.Transport{ - Proxy: http.ProxyURL(proxyURL), + Proxy: buildProxySelector(proxyURLs), } client := &http.Client{ diff --git a/backend/database.go b/backend/database.go index ec2fcf0..d747475 100644 --- a/backend/database.go +++ b/backend/database.go @@ -42,6 +42,15 @@ type AccountListItem struct { var db *sql.DB +type accountMetricsPayload struct { + AccountInfo struct { + FollowersCount int `json:"followers_count"` + StatusesCount int `json:"statuses_count"` + } `json:"account_info"` + Followers int `json:"followers"` + Posts int `json:"posts"` +} + func GetDBPath() string { homeDir, err := os.UserHomeDir() if err != nil { @@ -82,6 +91,8 @@ func InitDB() error { media_type TEXT DEFAULT 'all', cursor TEXT DEFAULT '', completed INTEGER DEFAULT 1, + followers_count INTEGER DEFAULT 0, + statuses_count INTEGER DEFAULT 0, UNIQUE(username, media_type) ) `) @@ -94,9 +105,15 @@ func InitDB() error { db.Exec("ALTER TABLE accounts ADD COLUMN media_type TEXT DEFAULT 'all'") db.Exec("ALTER TABLE accounts ADD COLUMN cursor TEXT DEFAULT ''") db.Exec("ALTER TABLE accounts ADD COLUMN completed INTEGER DEFAULT 1") + db.Exec("ALTER TABLE accounts ADD COLUMN followers_count INTEGER DEFAULT 0") + db.Exec("ALTER TABLE accounts ADD COLUMN statuses_count INTEGER DEFAULT 0") db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_username_media_type ON accounts(username, media_type)") + if err := backfillAccountMetrics(); err != nil { + return err + } + return nil } @@ -126,9 +143,11 @@ func SaveAccountWithStatus(username, name, profileImage string, totalMedia int, completedInt = 1 } + followersCount, statusesCount := extractAccountMetrics(responseJSON) + _, err := db.Exec(` - INSERT INTO accounts (username, name, profile_image, total_media, last_fetched, response_json, media_type, cursor, completed) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO accounts (username, name, profile_image, total_media, last_fetched, response_json, media_type, cursor, completed, followers_count, statuses_count) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(username, media_type) DO UPDATE SET name = excluded.name, profile_image = excluded.profile_image, @@ -136,8 +155,10 @@ func SaveAccountWithStatus(username, name, profileImage string, totalMedia int, last_fetched = excluded.last_fetched, response_json = excluded.response_json, cursor = excluded.cursor, - completed = excluded.completed - `, username, name, profileImage, totalMedia, time.Now(), responseJSON, mediaType, cursor, completedInt) + completed = excluded.completed, + followers_count = excluded.followers_count, + statuses_count = excluded.statuses_count + `, username, name, profileImage, totalMedia, time.Now(), responseJSON, mediaType, cursor, completedInt, followersCount, statusesCount) return err } @@ -154,7 +175,8 @@ func GetAllAccounts() ([]AccountListItem, error) { COALESCE(group_name, '') as group_name, COALESCE(group_color, '') as group_color, COALESCE(media_type, 'all') as media_type, COALESCE(cursor, '') as cursor, COALESCE(completed, 1) as completed, - COALESCE(response_json, '') as response_json + COALESCE(followers_count, 0) as followers_count, + COALESCE(statuses_count, 0) as statuses_count FROM accounts ORDER BY group_name ASC, last_fetched DESC `) @@ -168,33 +190,79 @@ func GetAllAccounts() ([]AccountListItem, error) { var acc AccountListItem var lastFetched time.Time var completedInt int - var responseJSON string - if err := rows.Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.GroupName, &acc.GroupColor, &acc.MediaType, &acc.Cursor, &completedInt, &responseJSON); err != nil { + if err := rows.Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.GroupName, &acc.GroupColor, &acc.MediaType, &acc.Cursor, &completedInt, &acc.FollowersCount, &acc.StatusesCount); err != nil { continue } acc.LastFetched = lastFetched.Format("2006-01-02 15:04") acc.Completed = completedInt == 1 - if responseJSON != "" { - var parsed map[string]interface{} - if err := json.Unmarshal([]byte(responseJSON), &parsed); err == nil { - if accountInfo, ok := parsed["account_info"].(map[string]interface{}); ok { - if followers, ok := accountInfo["followers_count"].(float64); ok { - acc.FollowersCount = int(followers) - } - if statuses, ok := accountInfo["statuses_count"].(float64); ok { - acc.StatusesCount = int(statuses) - } - } - } - } - accounts = append(accounts, acc) } return accounts, nil } +func extractAccountMetrics(responseJSON string) (followersCount int, statusesCount int) { + if responseJSON == "" { + return 0, 0 + } + + var payload accountMetricsPayload + if err := json.Unmarshal([]byte(responseJSON), &payload); err != nil { + return 0, 0 + } + + followersCount = payload.AccountInfo.FollowersCount + statusesCount = payload.AccountInfo.StatusesCount + + if followersCount == 0 { + followersCount = payload.Followers + } + if statusesCount == 0 { + statusesCount = payload.Posts + } + + return followersCount, statusesCount +} + +func backfillAccountMetrics() error { + rows, err := db.Query(` + SELECT id, COALESCE(response_json, '') as response_json + FROM accounts + WHERE COALESCE(response_json, '') != '' + AND (COALESCE(followers_count, 0) = 0 OR COALESCE(statuses_count, 0) = 0) + `) + if err != nil { + return err + } + defer rows.Close() + + stmt, err := db.Prepare(` + UPDATE accounts + SET followers_count = ?, statuses_count = ? + WHERE id = ? + `) + if err != nil { + return err + } + defer stmt.Close() + + for rows.Next() { + var id int64 + var responseJSON string + if err := rows.Scan(&id, &responseJSON); err != nil { + continue + } + + followersCount, statusesCount := extractAccountMetrics(responseJSON) + if _, err := stmt.Exec(followersCount, statusesCount, id); err != nil { + return err + } + } + + return rows.Err() +} + func UpdateAccountGroup(id int64, groupName, groupColor string) error { if db == nil { if err := InitDB(); err != nil { @@ -276,6 +344,41 @@ func GetAccountByID(id int64) (*AccountDB, error) { return &acc, nil } +func GetAccountByUsernameAndMediaType(username, mediaType string) (*AccountDB, error) { + if db == nil { + if err := InitDB(); err != nil { + return nil, err + } + } + + if mediaType == "" { + mediaType = "all" + } + + var acc AccountDB + var lastFetched time.Time + var completedInt int + err := db.QueryRow(` + SELECT id, username, name, profile_image, total_media, last_fetched, response_json, + COALESCE(media_type, 'all') as media_type, + COALESCE(cursor, '') as cursor, COALESCE(completed, 1) as completed + FROM accounts + WHERE LOWER(username) = LOWER(?) AND COALESCE(media_type, 'all') = ? + `, username, mediaType).Scan(&acc.ID, &acc.Username, &acc.Name, &acc.ProfileImage, &acc.TotalMedia, &lastFetched, &acc.ResponseJSON, &acc.MediaType, &acc.Cursor, &completedInt) + if err != nil { + return nil, err + } + + acc.LastFetched = lastFetched + acc.Completed = completedInt == 1 + + if converted, err := ConvertLegacyToNewFormat(acc.ResponseJSON); err == nil { + acc.ResponseJSON = converted + } + + return &acc, nil +} + func DeleteAccount(id int64) error { if db == nil { if err := InitDB(); err != nil { diff --git a/backend/dependency_version.go b/backend/dependency_version.go new file mode 100644 index 0000000..9097116 --- /dev/null +++ b/backend/dependency_version.go @@ -0,0 +1,7 @@ +package backend + +type DependencyVersionStatus struct { + Installed bool `json:"installed"` + InstalledVersion string `json:"installed_version,omitempty"` + LatestVersion string `json:"latest_version,omitempty"` +} diff --git a/backend/download.go b/backend/download.go index f1a74d6..6d60ef8 100644 --- a/backend/download.go +++ b/backend/download.go @@ -175,6 +175,12 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str } var status string + markCompleted := func() { + completed := atomic.AddInt64(&completedCount, 1) + if progress != nil { + progress(int(completed), total) + } + } if _, err := os.Stat(task.outputPath); err == nil { status = "skipped" @@ -183,6 +189,7 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str itemStatus(task.item.TweetID, task.index, status) } atomic.AddInt64(&skippedCount, 1) + markCompleted() continue } else if task.item.Type == "text" { @@ -214,10 +221,7 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str itemStatus(task.item.TweetID, task.index, status) } - completed := atomic.AddInt64(&completedCount, 1) - if progress != nil { - progress(int(completed), total) - } + markCompleted() } }() } diff --git a/backend/exiftool.go b/backend/exiftool.go index 823be75..5a4b6e8 100644 --- a/backend/exiftool.go +++ b/backend/exiftool.go @@ -2,32 +2,56 @@ package backend import ( "archive/zip" - "crypto/sha256" - "encoding/hex" + "encoding/json" + "encoding/xml" "fmt" + "html" "io" "net/http" "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" + "time" ) const ( - exiftoolWindows64URL = "https://sourceforge.net/projects/exiftool/files/exiftool-13.43_64.zip/download" - - exiftoolWindows32URL = "https://sourceforge.net/projects/exiftool/files/exiftool-13.43_32.zip/download" - - exiftoolUnixURL = "https://sourceforge.net/projects/exiftool/files/Image-ExifTool-13.43.tar.gz/download" + exiftoolBestReleaseAPIURL = "https://sourceforge.net/projects/exiftool/best_release.json" + exiftoolRSSURL = "https://sourceforge.net/projects/exiftool/rss?path=/" + exiftoolFilesBaseURL = "https://sourceforge.net/projects/exiftool/files" + sourceForgeUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36" ) -const ( - exiftoolWindows64Hash = "" - exiftoolWindows32Hash = "" - exiftoolUnixHash = "" +var ( + exiftoolVersionPattern = regexp.MustCompile(`(?i)(?:exiftool|image-exiftool)-([0-9]+(?:\.[0-9]+)+)`) + sourceForgeMirrorMetaPattern = regexp.MustCompile(`(?i)url=(https?://downloads\.sourceforge\.net/project/[^"'<> ]+)`) + sourceForgeMirrorURLPattern = regexp.MustCompile(`https?://downloads\.sourceforge\.net/project/[^"'<> ]+`) ) +type sourceForgeBestRelease struct { + Release sourceForgeBestReleaseFile `json:"release"` +} + +type sourceForgeBestReleaseFile struct { + Filename string `json:"filename"` + URL string `json:"url"` +} + +type sourceForgeRSS struct { + Channel sourceForgeRSSChannel `xml:"channel"` +} + +type sourceForgeRSSChannel struct { + Items []sourceForgeRSSItem `xml:"item"` +} + +type sourceForgeRSSItem struct { + Title string `xml:"title"` + Link string `xml:"link"` +} + func GetExifToolPath() string { homeDir, _ := os.UserHomeDir() baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader") @@ -36,11 +60,9 @@ func GetExifToolPath() string { case "windows": return filepath.Join(baseDir, "exiftool.exe") default: - pattern := filepath.Join(baseDir, "Image-ExifTool-*") matches, err := filepath.Glob(pattern) if err == nil && len(matches) > 0 { - exiftoolPath := filepath.Join(matches[0], "exiftool") if _, err := os.Stat(exiftoolPath); err == nil { return exiftoolPath @@ -52,44 +74,74 @@ func GetExifToolPath() string { } func IsExifToolInstalled() bool { - exiftoolPath := GetExifToolPath() - if _, err := os.Stat(exiftoolPath); err == nil { + _, err := getInstalledExifToolVersion() + return err == nil +} - cmd := exec.Command(exiftoolPath, "-ver") - hideWindow(cmd) - if err := cmd.Run(); err == nil { - return true +func GetExifToolVersionStatus() DependencyVersionStatus { + status := DependencyVersionStatus{ + Installed: IsExifToolInstalled(), + } + + if status.Installed { + if installedVersion, err := getInstalledExifToolVersion(); err == nil { + status.InstalledVersion = installedVersion } } - return false + + if latestVersion, err := fetchLatestExifToolVersion(); err == nil { + status.LatestVersion = latestVersion + } + + return status } -func calculateSHA256(filePath string) (string, error) { - file, err := os.Open(filePath) +func getInstalledExifToolVersion() (string, error) { + exiftoolPath := GetExifToolPath() + if _, err := os.Stat(exiftoolPath); err != nil { + return "", fmt.Errorf("exiftool not installed") + } + + cmd := exec.Command(exiftoolPath, "-ver") + hideWindow(cmd) + output, err := cmd.Output() if err != nil { - return "", err - } - defer file.Close() - - hash := sha256.New() - if _, err := io.Copy(hash, file); err != nil { - return "", err + return "", fmt.Errorf("failed to determine exiftool version: %v", err) } - return hex.EncodeToString(hash.Sum(nil)), nil + version := normalizeExifToolVersion(string(output)) + if version == "" { + return "", fmt.Errorf("failed to determine exiftool version") + } + + return version, nil } -func verifyHash(filePath, expectedHash string) error { - actualHash, err := calculateSHA256(filePath) - if err != nil { - return fmt.Errorf("failed to calculate hash: %v", err) +func normalizeExifToolVersion(version string) string { + version = strings.TrimSpace(version) + if version == "" { + return "" } - if !strings.EqualFold(actualHash, expectedHash) { - return fmt.Errorf("hash verification failed: expected %s, got %s", expectedHash, actualHash) + fields := strings.Fields(version) + if len(fields) > 0 { + version = fields[0] } - return nil + if strings.HasPrefix(strings.ToLower(version), "v") { + version = strings.TrimPrefix(strings.TrimPrefix(version, "v"), "V") + } + + if version == "" { + return "" + } + + first := version[0] + if first >= '0' && first <= '9' { + return "v" + version + } + + return version } func is64Bit() bool { @@ -97,24 +149,14 @@ func is64Bit() bool { } func DownloadExifTool(progressCallback func(downloaded, total int64)) error { - var downloadURL string - var expectedHash string + latestVersion, err := fetchLatestExifToolVersion() + if err != nil { + return err + } - switch runtime.GOOS { - case "windows": - if is64Bit() { - downloadURL = exiftoolWindows64URL - expectedHash = exiftoolWindows64Hash - } else { - downloadURL = exiftoolWindows32URL - expectedHash = exiftoolWindows32Hash - } - case "linux", "darwin": - - downloadURL = exiftoolUnixURL - expectedHash = exiftoolUnixHash - default: - return fmt.Errorf("unsupported platform: %s", runtime.GOOS) + downloadURL, err := buildLatestExifToolDownloadURL(latestVersion) + if err != nil { + return err } tempFile, err := os.CreateTemp("", "exiftool-*") @@ -123,47 +165,12 @@ func DownloadExifTool(progressCallback func(downloaded, total int64)) error { } tempPath := tempFile.Name() defer os.Remove(tempPath) - defer tempFile.Close() - - resp, err := http.Get(downloadURL) - if err != nil { - return fmt.Errorf("failed to download exiftool: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("failed to download exiftool: status %d", resp.StatusCode) + if err := tempFile.Close(); err != nil { + return fmt.Errorf("failed to prepare temp file: %v", err) } - total := resp.ContentLength - var downloaded int64 - buf := make([]byte, 32*1024) - - for { - n, err := resp.Body.Read(buf) - if n > 0 { - _, writeErr := tempFile.Write(buf[:n]) - if writeErr != nil { - return fmt.Errorf("failed to write temp file: %v", writeErr) - } - downloaded += int64(n) - if progressCallback != nil { - progressCallback(downloaded, total) - } - } - if err == io.EOF { - break - } - if err != nil { - return fmt.Errorf("failed to download: %v", err) - } - } - tempFile.Close() - - if expectedHash != "" { - if err := verifyHash(tempPath, expectedHash); err != nil { - return fmt.Errorf("hash verification failed: %v", err) - } + if err := downloadSourceForgeFile(downloadURL, tempPath, progressCallback); err != nil { + return err } exiftoolPath := GetExifToolPath() @@ -176,13 +183,254 @@ func DownloadExifTool(progressCallback func(downloaded, total int64)) error { case "windows": return extractExifToolFromZip(tempPath, exiftoolPath) case "linux", "darwin": - return extractExifToolFromTarGz(tempPath, exiftoolPath) } return nil } +func fetchLatestExifToolVersion() (string, error) { + version, err := fetchLatestExifToolVersionFromBestRelease() + if err == nil { + return version, nil + } + + fallbackVersion, fallbackErr := fetchLatestExifToolVersionFromRSS() + if fallbackErr == nil { + return fallbackVersion, nil + } + + return "", fmt.Errorf("failed to fetch latest exiftool version: %v; fallback failed: %v", err, fallbackErr) +} + +func fetchLatestExifToolVersionFromBestRelease() (string, error) { + client, err := CreateHTTPClient("", 30*time.Second) + if err != nil { + return "", fmt.Errorf("failed to configure exiftool release client: %v", err) + } + + req, err := http.NewRequest(http.MethodGet, exiftoolBestReleaseAPIURL, nil) + if err != nil { + return "", fmt.Errorf("failed to create exiftool release request: %v", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", sourceForgeUserAgent) + + resp, err := client.Do(req) + if err != nil { + return "", fmt.Errorf("failed to fetch exiftool release info: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to fetch exiftool release info: status %d", resp.StatusCode) + } + + var bestRelease sourceForgeBestRelease + if err := json.NewDecoder(resp.Body).Decode(&bestRelease); err != nil { + return "", fmt.Errorf("failed to decode exiftool release info: %v", err) + } + + version := parseExifToolVersion(bestRelease.Release.Filename) + if version == "" { + version = parseExifToolVersion(bestRelease.Release.URL) + } + if version == "" { + return "", fmt.Errorf("failed to parse exiftool version from best_release.json") + } + + return version, nil +} + +func fetchLatestExifToolVersionFromRSS() (string, error) { + client, err := CreateHTTPClient("", 30*time.Second) + if err != nil { + return "", fmt.Errorf("failed to configure exiftool RSS client: %v", err) + } + + req, err := http.NewRequest(http.MethodGet, exiftoolRSSURL, nil) + if err != nil { + return "", fmt.Errorf("failed to create exiftool RSS request: %v", err) + } + req.Header.Set("Accept", "application/rss+xml, application/xml, text/xml") + req.Header.Set("User-Agent", sourceForgeUserAgent) + + resp, err := client.Do(req) + if err != nil { + return "", fmt.Errorf("failed to fetch exiftool RSS: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to fetch exiftool RSS: status %d", resp.StatusCode) + } + + var feed sourceForgeRSS + if err := xml.NewDecoder(resp.Body).Decode(&feed); err != nil { + return "", fmt.Errorf("failed to decode exiftool RSS: %v", err) + } + + for _, item := range feed.Channel.Items { + version := parseExifToolVersion(item.Title) + if version == "" { + version = parseExifToolVersion(item.Link) + } + if version != "" { + return version, nil + } + } + + return "", fmt.Errorf("failed to find exiftool version in RSS feed") +} + +func parseExifToolVersion(value string) string { + matches := exiftoolVersionPattern.FindStringSubmatch(value) + if len(matches) < 2 { + return "" + } + + return normalizeExifToolVersion(matches[1]) +} + +func buildLatestExifToolDownloadURL(version string) (string, error) { + version = normalizeExifToolVersion(version) + if version == "" { + return "", fmt.Errorf("missing exiftool version") + } + version = strings.TrimPrefix(strings.TrimPrefix(version, "v"), "V") + + switch runtime.GOOS { + case "windows": + if is64Bit() { + return fmt.Sprintf("%s/exiftool-%s_64.zip/download", exiftoolFilesBaseURL, version), nil + } + return fmt.Sprintf("%s/exiftool-%s_32.zip/download", exiftoolFilesBaseURL, version), nil + case "linux", "darwin": + return fmt.Sprintf("%s/Image-ExifTool-%s.tar.gz/download", exiftoolFilesBaseURL, version), nil + default: + return "", fmt.Errorf("unsupported platform: %s", runtime.GOOS) + } +} + +func downloadSourceForgeFile(downloadURL, destinationPath string, progressCallback func(downloaded, total int64)) error { + client, err := CreateHTTPClient("", 10*time.Minute) + if err != nil { + return fmt.Errorf("failed to configure SourceForge download client: %v", err) + } + + err = downloadSourceForgeURL(client, downloadURL, destinationPath, progressCallback, 0) + if err == nil { + return nil + } + + if runtime.GOOS == "windows" { + if fallbackErr := downloadSourceForgeFileWithCurl(downloadURL, destinationPath); fallbackErr == nil { + return nil + } else { + return fmt.Errorf("%v; curl fallback failed: %v", err, fallbackErr) + } + } + + return err +} + +func downloadSourceForgeURL(client *http.Client, downloadURL, destinationPath string, progressCallback func(downloaded, total int64), depth int) error { + if depth > 4 { + return fmt.Errorf("failed to resolve SourceForge mirror URL") + } + + req, err := http.NewRequest(http.MethodGet, downloadURL, nil) + if err != nil { + return fmt.Errorf("failed to create SourceForge download request: %v", err) + } + req.Header.Set("User-Agent", sourceForgeUserAgent) + + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("failed to download exiftool: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("failed to download exiftool: status %d", resp.StatusCode) + } + + contentType := strings.ToLower(resp.Header.Get("Content-Type")) + if strings.Contains(contentType, "text/html") { + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read SourceForge download page: %v", err) + } + + resolvedURL := extractSourceForgeMirrorURL(string(body)) + if resolvedURL == "" { + return fmt.Errorf("failed to resolve SourceForge mirror URL from download page") + } + + return downloadSourceForgeURL(client, resolvedURL, destinationPath, progressCallback, depth+1) + } + + destination, err := os.OpenFile(destinationPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + return fmt.Errorf("failed to create exiftool archive: %v", err) + } + defer destination.Close() + + total := resp.ContentLength + var downloaded int64 + buf := make([]byte, 32*1024) + + for { + n, err := resp.Body.Read(buf) + if n > 0 { + if _, writeErr := destination.Write(buf[:n]); writeErr != nil { + return fmt.Errorf("failed to write exiftool archive: %v", writeErr) + } + downloaded += int64(n) + if progressCallback != nil { + progressCallback(downloaded, total) + } + } + if err == io.EOF { + break + } + if err != nil { + return fmt.Errorf("failed to download exiftool: %v", err) + } + } + + return nil +} + +func downloadSourceForgeFileWithCurl(downloadURL, destinationPath string) error { + curlPath, err := exec.LookPath("curl.exe") + if err != nil { + curlPath, err = exec.LookPath("curl") + if err != nil { + return fmt.Errorf("curl executable not found") + } + } + + cmd := exec.Command(curlPath, "-L", "-o", destinationPath, downloadURL) + hideWindow(cmd) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to download via curl: %v, output: %s", err, string(output)) + } + + return nil +} + +func extractSourceForgeMirrorURL(page string) string { + page = html.UnescapeString(page) + + if matches := sourceForgeMirrorMetaPattern.FindStringSubmatch(page); len(matches) > 1 { + return strings.TrimSpace(matches[1]) + } + + return strings.TrimSpace(sourceForgeMirrorURLPattern.FindString(page)) +} + func extractExifToolFromZip(zipPath, destPath string) error { r, err := zip.OpenReader(zipPath) if err != nil { @@ -222,9 +470,7 @@ func extractExifToolFromZip(zipPath, destPath string) error { libDir := filepath.Join(filepath.Dir(destPath), "exiftool_files") for _, f := range r.File { - if strings.Contains(f.Name, "exiftool_files") && !strings.HasSuffix(f.Name, "/") { - parts := strings.Split(f.Name, "exiftool_files/") if len(parts) < 2 { continue @@ -266,7 +512,6 @@ func extractExifToolFromZip(zipPath, destPath string) error { } func extractExifToolFromTarGz(tarGzPath, destPath string) error { - baseDir := filepath.Dir(destPath) cmd := exec.Command("tar", "-xzf", tarGzPath, "-C", baseDir) @@ -282,22 +527,15 @@ func extractExifToolFromTarGz(tarGzPath, destPath string) error { pattern := filepath.Join(baseDir, "Image-ExifTool-*") matches, err := filepath.Glob(pattern) if err == nil && len(matches) > 0 { - extractedDir = matches[0] exiftoolScript = filepath.Join(extractedDir, "exiftool") } else { - - extractedDir = filepath.Join(baseDir, "Image-ExifTool-13.43") + extractedDir = filepath.Join(baseDir, "exiftool") exiftoolScript = filepath.Join(extractedDir, "exiftool") } if _, err := os.Stat(exiftoolScript); err != nil { - - extractedDir = filepath.Join(baseDir, "exiftool") - exiftoolScript = filepath.Join(extractedDir, "exiftool") - if _, err := os.Stat(exiftoolScript); err != nil { - return fmt.Errorf("exiftool script not found in extracted archive (searched in: %s and %s)", pattern, extractedDir) - } + return fmt.Errorf("exiftool script not found in extracted archive (searched in: %s and %s)", pattern, extractedDir) } if err := os.Chmod(exiftoolScript, 0755); err != nil { diff --git a/backend/extractor.go b/backend/extractor.go index 3950844..3fbca43 100644 --- a/backend/extractor.go +++ b/backend/extractor.go @@ -17,6 +17,19 @@ import ( ) const extractorReleaseAPIURL = "https://api.github.com/repos/afkarxyz/xtractor-binaries/releases/latest" +const extractorReleasesAPIURL = "https://api.github.com/repos/afkarxyz/xtractor-binaries/releases" +const extractorReleasePageSize = 20 + +type ExtractorVersionStatus struct { + Installed bool `json:"installed"` + InstalledVersion string `json:"installed_version,omitempty"` + LatestVersion string `json:"latest_version,omitempty"` +} + +type extractorVersionMetadata struct { + Version string `json:"version"` + AssetName string `json:"asset_name,omitempty"` +} type githubRelease struct { TagName string `json:"tag_name"` @@ -34,6 +47,25 @@ func IsExtractorInstalled() bool { return err == nil && !info.IsDir() } +func GetExtractorVersionStatus() ExtractorVersionStatus { + status := ExtractorVersionStatus{ + Installed: IsExtractorInstalled(), + } + + assetName, assetErr := getExtractorAssetName() + if status.Installed && assetErr == nil { + if installedVersion, err := getInstalledExtractorVersion(assetName); err == nil { + status.InstalledVersion = installedVersion + } + } + + if latestRelease, err := fetchLatestExtractorRelease(); err == nil { + status.LatestVersion = normalizeExtractorVersion(latestRelease.TagName) + } + + return status +} + func DownloadExtractor(progressCallback func(downloaded, total int64)) error { assetName, err := getExtractorAssetName() if err != nil { @@ -82,6 +114,10 @@ func DownloadExtractor(progressCallback func(downloaded, total int64)) error { } _ = removeLegacyExtractorBinary() + _ = writeExtractorVersionMetadata(extractorVersionMetadata{ + Version: normalizeExtractorVersion(release.TagName), + AssetName: asset.Name, + }) return nil } @@ -157,12 +193,10 @@ func fetchLatestExtractorRelease() (*githubRelease, error) { return nil, fmt.Errorf("failed to configure xtractor network client: %v", err) } - req, err := http.NewRequest(http.MethodGet, extractorReleaseAPIURL, nil) + req, err := newExtractorGitHubRequest(extractorReleaseAPIURL) if err != nil { - return nil, fmt.Errorf("failed to create xtractor release request: %v", err) + return nil, err } - req.Header.Set("Accept", "application/vnd.github+json") - req.Header.Set("User-Agent", "Twitter-X-Media-Batch-Downloader") resp, err := client.Do(req) if err != nil { @@ -182,6 +216,46 @@ func fetchLatestExtractorRelease() (*githubRelease, error) { return &release, nil } +func fetchExtractorReleases(limit int) ([]githubRelease, error) { + client, err := CreateHTTPClient("", 60*time.Second) + if err != nil { + return nil, fmt.Errorf("failed to configure xtractor release client: %v", err) + } + + req, err := newExtractorGitHubRequest(fmt.Sprintf("%s?per_page=%d", extractorReleasesAPIURL, limit)) + if err != nil { + return nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch xtractor releases: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to fetch xtractor releases: status %d", resp.StatusCode) + } + + var releases []githubRelease + if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { + return nil, fmt.Errorf("failed to decode xtractor releases: %v", err) + } + + return releases, nil +} + +func newExtractorGitHubRequest(url string) (*http.Request, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create xtractor GitHub request: %v", err) + } + + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("User-Agent", "Twitter-X-Media-Batch-Downloader") + return req, nil +} + func findExtractorAsset(assets []githubReleaseAsset, assetName string) (*githubReleaseAsset, error) { for i := range assets { if assets[i].Name == assetName { @@ -348,6 +422,177 @@ func findExtractorBinaryEntry(entries []*zip.File) (*zip.File, error) { return nil, fmt.Errorf("xtractor binary not found in archive") } +func getInstalledExtractorVersion(assetName string) (string, error) { + metadata, err := readExtractorVersionMetadata() + if err == nil { + version := normalizeExtractorVersion(metadata.Version) + if version != "" && (metadata.AssetName == "" || strings.EqualFold(metadata.AssetName, assetName)) { + return version, nil + } + } + + version, err := resolveInstalledExtractorVersionByHash(assetName) + if err != nil { + return "", err + } + + version = normalizeExtractorVersion(version) + if version != "" { + _ = writeExtractorVersionMetadata(extractorVersionMetadata{ + Version: version, + AssetName: assetName, + }) + } + + return version, nil +} + +func resolveInstalledExtractorVersionByHash(assetName string) (string, error) { + exePath, err := requireExtractorPath() + if err != nil { + return "", err + } + + installedHash, err := hashFileSHA256(exePath) + if err != nil { + return "", fmt.Errorf("failed to hash installed xtractor: %v", err) + } + + releases, err := fetchExtractorReleases(extractorReleasePageSize) + if err != nil { + return "", err + } + + for _, release := range releases { + asset, err := findExtractorAsset(release.Assets, assetName) + if err != nil { + continue + } + + matches, err := archiveMatchesExtractorHash(asset.BrowserDownloadURL, installedHash) + if err != nil { + continue + } + + if matches { + return release.TagName, nil + } + } + + return "", nil +} + +func archiveMatchesExtractorHash(downloadURL, installedHash string) (bool, error) { + tempFile, err := os.CreateTemp("", "xtractor-version-*.zip") + if err != nil { + return false, fmt.Errorf("failed to create temp archive for version check: %v", err) + } + + tempPath := tempFile.Name() + defer os.Remove(tempPath) + defer tempFile.Close() + + if err := downloadExtractorArchive(downloadURL, tempFile, nil); err != nil { + return false, err + } + + if err := tempFile.Close(); err != nil { + return false, fmt.Errorf("failed to finalize version archive: %v", err) + } + + archiveHash, err := hashExtractorBinaryInArchive(tempPath) + if err != nil { + return false, err + } + + return archiveHash == installedHash, nil +} + +func hashExtractorBinaryInArchive(zipPath string) (string, error) { + reader, err := zip.OpenReader(zipPath) + if err != nil { + return "", fmt.Errorf("failed to open xtractor archive for version check: %v", err) + } + defer reader.Close() + + entry, err := findExtractorBinaryEntry(reader.File) + if err != nil { + return "", err + } + + in, err := entry.Open() + if err != nil { + return "", fmt.Errorf("failed to open xtractor binary from archive for version check: %v", err) + } + defer in.Close() + + return hashReaderSHA256(in) +} + +func hashFileSHA256(filePath string) (string, error) { + file, err := os.Open(filePath) + if err != nil { + return "", fmt.Errorf("failed to open xtractor binary: %v", err) + } + defer file.Close() + + return hashReaderSHA256(file) +} + +func hashReaderSHA256(reader io.Reader) (string, error) { + hash := sha256.New() + if _, err := io.Copy(hash, reader); err != nil { + return "", err + } + + return hex.EncodeToString(hash.Sum(nil)), nil +} + +func getExtractorVersionMetadataPath() string { + return filepath.Join(filepath.Dir(getExtractorPath()), "xtractor-version.json") +} + +func readExtractorVersionMetadata() (extractorVersionMetadata, error) { + data, err := os.ReadFile(getExtractorVersionMetadataPath()) + if err != nil { + return extractorVersionMetadata{}, err + } + + var metadata extractorVersionMetadata + if err := json.Unmarshal(data, &metadata); err != nil { + return extractorVersionMetadata{}, err + } + + return metadata, nil +} + +func writeExtractorVersionMetadata(metadata extractorVersionMetadata) error { + metadata.Version = normalizeExtractorVersion(metadata.Version) + if metadata.Version == "" { + return nil + } + + path := getExtractorVersionMetadataPath() + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return fmt.Errorf("failed to create xtractor metadata directory: %v", err) + } + + data, err := json.Marshal(metadata) + if err != nil { + return fmt.Errorf("failed to encode xtractor metadata: %v", err) + } + + if err := os.WriteFile(path, data, 0644); err != nil { + return fmt.Errorf("failed to write xtractor metadata: %v", err) + } + + return nil +} + +func normalizeExtractorVersion(version string) string { + return strings.TrimSpace(version) +} + func getLegacyExtractorPath() string { homeDir, _ := os.UserHomeDir() baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader") diff --git a/backend/ffmpeg.go b/backend/ffmpeg.go index 36f8b10..2a4608b 100644 --- a/backend/ffmpeg.go +++ b/backend/ffmpeg.go @@ -1,26 +1,41 @@ package backend import ( - "archive/tar" "archive/zip" + "encoding/json" "fmt" "io" "net/http" + neturl "net/url" "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" - - "github.com/ulikunitz/xz" + "time" ) const ( - ffmpegWindowsURL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip" - ffmpegLinuxURL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz" - ffmpegMacOSURL = "https://evermeet.cx/ffmpeg/getrelease/ffmpeg/zip" + ffmpegReleaseAPIURL = "https://api.github.com/repos/afkarxyz/ffmpeg-binaries/releases/latest" + ffmpegLatestReleaseURL = "https://github.com/afkarxyz/ffmpeg-binaries/releases/latest" + ffmpegLatestDownloadBaseURL = "https://github.com/afkarxyz/ffmpeg-binaries/releases/latest/download" ) +var ffmpegVersionPattern = regexp.MustCompile(`^[0-9]+(?:\.[0-9]+)+`) + +type ffmpegVersionMetadata struct { + Version string `json:"version"` + AssetName string `json:"asset_name,omitempty"` +} + +type ffmpegResolvedRelease struct { + Version string + AssetURL string + AssetName string + Digest string +} + func GetFFmpegPath() string { homeDir, _ := os.UserHomeDir() baseDir := filepath.Join(homeDir, ".twitterxmediabatchdownloader") @@ -35,27 +50,45 @@ func GetFFmpegPath() string { func IsFFmpegInstalled() bool { ffmpegPath := GetFFmpegPath() - if _, err := os.Stat(ffmpegPath); err == nil { - return true + if _, err := os.Stat(ffmpegPath); err != nil { + return false } - return false + + cmd := exec.Command(ffmpegPath, "-version") + hideWindow(cmd) + return cmd.Run() == nil +} + +func GetFFmpegVersionStatus() DependencyVersionStatus { + status := DependencyVersionStatus{ + Installed: IsFFmpegInstalled(), + } + + if status.Installed { + if installedVersion, err := getInstalledFFmpegVersion(); err == nil { + status.InstalledVersion = installedVersion + } + } + + if latestVersion, err := fetchLatestFFmpegVersion(); err == nil { + status.LatestVersion = latestVersion + } + + return status } func DownloadFFmpeg(progressCallback func(downloaded, total int64)) error { - var downloadURL string - - switch runtime.GOOS { - case "windows": - downloadURL = ffmpegWindowsURL - case "linux": - downloadURL = ffmpegLinuxURL - case "darwin": - downloadURL = ffmpegMacOSURL - default: - return fmt.Errorf("unsupported platform: %s", runtime.GOOS) + assetName, err := getFFmpegAssetName() + if err != nil { + return err } - tempFile, err := os.CreateTemp("", "ffmpeg-*") + releaseInfo, err := resolveLatestFFmpegRelease(assetName) + if err != nil { + return err + } + + tempFile, err := os.CreateTemp("", "ffmpeg-*.zip") if err != nil { return fmt.Errorf("failed to create temp file: %v", err) } @@ -63,7 +96,227 @@ func DownloadFFmpeg(progressCallback func(downloaded, total int64)) error { defer os.Remove(tempPath) defer tempFile.Close() - resp, err := http.Get(downloadURL) + if err := downloadFFmpegArchive(releaseInfo.AssetURL, tempFile, progressCallback); err != nil { + return err + } + + if err := tempFile.Close(); err != nil { + return fmt.Errorf("failed to finalize ffmpeg archive: %v", err) + } + + if releaseInfo.Digest != "" { + if err := verifyDownloadedDigest(tempPath, releaseInfo.Digest); err != nil { + return err + } + } + + ffmpegPath := GetFFmpegPath() + baseDir := filepath.Dir(ffmpegPath) + if err := os.MkdirAll(baseDir, 0755); err != nil { + return fmt.Errorf("failed to create ffmpeg directory: %v", err) + } + + if err := extractFFmpegFromZip(tempPath, ffmpegPath); err != nil { + return err + } + + _ = writeFFmpegVersionMetadata(ffmpegVersionMetadata{ + Version: releaseInfo.Version, + AssetName: releaseInfo.AssetName, + }) + + return nil +} + +func getFFmpegAssetName() (string, error) { + switch runtime.GOOS { + case "windows": + return "ffmpeg-windows.zip", nil + case "linux": + switch runtime.GOARCH { + case "amd64": + return "ffmpeg-linux-amd64.zip", nil + case "arm64": + return "ffmpeg-linux-arm64v8.zip", nil + } + case "darwin": + switch runtime.GOARCH { + case "amd64": + return "ffmpeg-macos-amd64.zip", nil + case "arm64": + return "ffmpeg-macos-arm64.zip", nil + } + } + + return "", fmt.Errorf("unsupported platform for ffmpeg binary: %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func resolveLatestFFmpegRelease(assetName string) (ffmpegResolvedRelease, error) { + release, apiErr := fetchLatestFFmpegRelease() + if apiErr == nil { + asset, assetErr := findFFmpegAsset(release.Assets, assetName) + if assetErr == nil { + return ffmpegResolvedRelease{ + Version: normalizeFFmpegVersion(release.TagName), + AssetURL: asset.BrowserDownloadURL, + AssetName: asset.Name, + Digest: asset.Digest, + }, nil + } + apiErr = assetErr + } + + fallbackVersion, fallbackErr := fetchLatestFFmpegVersionFromRedirect() + if fallbackErr != nil { + return ffmpegResolvedRelease{}, fmt.Errorf("failed to resolve latest ffmpeg release: %v; fallback failed: %v", apiErr, fallbackErr) + } + + return ffmpegResolvedRelease{ + Version: fallbackVersion, + AssetURL: buildLatestFFmpegDownloadURL(assetName), + AssetName: assetName, + }, nil +} + +func fetchLatestFFmpegVersion() (string, error) { + release, err := fetchLatestFFmpegRelease() + if err == nil { + version := normalizeFFmpegVersion(release.TagName) + if version != "" { + return version, nil + } + } + + fallbackVersion, fallbackErr := fetchLatestFFmpegVersionFromRedirect() + if fallbackErr == nil { + return fallbackVersion, nil + } + + if err != nil { + return "", fmt.Errorf("failed to fetch latest ffmpeg version: %v; fallback failed: %v", err, fallbackErr) + } + + return "", fmt.Errorf("failed to determine latest ffmpeg version") +} + +func fetchLatestFFmpegRelease() (*githubRelease, error) { + client, err := CreateHTTPClient("", 30*time.Second) + if err != nil { + return nil, fmt.Errorf("failed to configure ffmpeg network client: %v", err) + } + + req, err := newFFmpegGitHubRequest(ffmpegReleaseAPIURL) + if err != nil { + return nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch ffmpeg release info: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to fetch ffmpeg release info: status %d", resp.StatusCode) + } + + var release githubRelease + if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { + return nil, fmt.Errorf("failed to decode ffmpeg release info: %v", err) + } + + return &release, nil +} + +func newFFmpegGitHubRequest(url string) (*http.Request, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create ffmpeg GitHub request: %v", err) + } + + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("User-Agent", "Twitter-X-Media-Batch-Downloader") + return req, nil +} + +func findFFmpegAsset(assets []githubReleaseAsset, assetName string) (*githubReleaseAsset, error) { + for i := range assets { + if assets[i].Name == assetName { + return &assets[i], nil + } + } + + return nil, fmt.Errorf("ffmpeg asset %q not found in latest release", assetName) +} + +func fetchLatestFFmpegVersionFromRedirect() (string, error) { + client, err := CreateHTTPClient("", 30*time.Second) + if err != nil { + return "", fmt.Errorf("failed to configure ffmpeg redirect client: %v", err) + } + + client.CheckRedirect = func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + } + + req, err := http.NewRequest(http.MethodGet, ffmpegLatestReleaseURL, nil) + if err != nil { + return "", fmt.Errorf("failed to create ffmpeg redirect request: %v", err) + } + req.Header.Set("User-Agent", "Twitter-X-Media-Batch-Downloader") + + resp, err := client.Do(req) + if err != nil { + return "", fmt.Errorf("failed to resolve latest ffmpeg redirect: %v", err) + } + defer resp.Body.Close() + + location := resp.Header.Get("Location") + if location == "" && resp.Request != nil && resp.Request.URL != nil { + location = resp.Request.URL.String() + } + + version := parseFFmpegVersionFromReleaseURL(location) + if version == "" { + return "", fmt.Errorf("failed to parse ffmpeg version from redirect URL") + } + + return version, nil +} + +func parseFFmpegVersionFromReleaseURL(releaseURL string) string { + parsed, err := neturl.Parse(strings.TrimSpace(releaseURL)) + if err != nil { + return "" + } + + segments := strings.Split(strings.Trim(parsed.Path, "/"), "/") + for index, segment := range segments { + if (segment == "tag" || segment == "download") && index+1 < len(segments) { + return normalizeFFmpegVersion(segments[index+1]) + } + } + + return "" +} + +func buildLatestFFmpegDownloadURL(assetName string) string { + return ffmpegLatestDownloadBaseURL + "/" + assetName +} + +func downloadFFmpegArchive(downloadURL string, destination *os.File, progressCallback func(downloaded, total int64)) error { + client, err := CreateHTTPClient("", 10*time.Minute) + if err != nil { + return fmt.Errorf("failed to configure ffmpeg download client: %v", err) + } + + req, err := http.NewRequest(http.MethodGet, downloadURL, nil) + if err != nil { + return fmt.Errorf("failed to create ffmpeg download request: %v", err) + } + req.Header.Set("User-Agent", "Twitter-X-Media-Batch-Downloader") + + resp, err := client.Do(req) if err != nil { return fmt.Errorf("failed to download ffmpeg: %v", err) } @@ -80,9 +333,8 @@ func DownloadFFmpeg(progressCallback func(downloaded, total int64)) error { for { n, err := resp.Body.Read(buf) if n > 0 { - _, writeErr := tempFile.Write(buf[:n]) - if writeErr != nil { - return fmt.Errorf("failed to write temp file: %v", writeErr) + if _, writeErr := destination.Write(buf[:n]); writeErr != nil { + return fmt.Errorf("failed to write ffmpeg archive: %v", writeErr) } downloaded += int64(n) if progressCallback != nil { @@ -93,28 +345,14 @@ func DownloadFFmpeg(progressCallback func(downloaded, total int64)) error { break } if err != nil { - return fmt.Errorf("failed to download: %v", err) + return fmt.Errorf("failed to download ffmpeg: %v", err) } } - tempFile.Close() - - ffmpegPath := GetFFmpegPath() - baseDir := filepath.Dir(ffmpegPath) - if err := os.MkdirAll(baseDir, 0755); err != nil { - return fmt.Errorf("failed to create directory: %v", err) - } - - switch runtime.GOOS { - case "windows", "darwin": - return extractFromZip(tempPath, ffmpegPath) - case "linux": - return extractFromTarXz(tempPath, ffmpegPath) - } return nil } -func extractFromZip(zipPath, destPath string) error { +func extractFFmpegFromZip(zipPath, destPath string) error { r, err := zip.OpenReader(zipPath) if err != nil { return fmt.Errorf("failed to open zip: %v", err) @@ -122,77 +360,174 @@ func extractFromZip(zipPath, destPath string) error { defer r.Close() for _, f := range r.File { - name := filepath.Base(f.Name) - if name == "ffmpeg" || name == "ffmpeg.exe" { - rc, err := f.Open() - if err != nil { - return fmt.Errorf("failed to open file in zip: %v", err) - } - defer rc.Close() - - out, err := os.Create(destPath) - if err != nil { - return fmt.Errorf("failed to create output file: %v", err) - } - defer out.Close() - - if _, err := io.Copy(out, rc); err != nil { - return fmt.Errorf("failed to extract file: %v", err) - } - - if runtime.GOOS != "windows" { - os.Chmod(destPath, 0755) - } - - return nil + if name != "ffmpeg" && name != "ffmpeg.exe" { + continue } + + rc, err := f.Open() + if err != nil { + return fmt.Errorf("failed to open file in zip: %v", err) + } + defer rc.Close() + + out, err := os.Create(destPath) + if err != nil { + return fmt.Errorf("failed to create output file: %v", err) + } + defer out.Close() + + if _, err := io.Copy(out, rc); err != nil { + return fmt.Errorf("failed to extract file: %v", err) + } + + if runtime.GOOS != "windows" { + if err := os.Chmod(destPath, 0755); err != nil { + return fmt.Errorf("failed to make ffmpeg executable: %v", err) + } + } + + return nil } return fmt.Errorf("ffmpeg binary not found in archive") } -func extractFromTarXz(tarXzPath, destPath string) error { - file, err := os.Open(tarXzPath) +func getInstalledFFmpegVersion() (string, error) { + ffmpegPath := GetFFmpegPath() + if _, err := os.Stat(ffmpegPath); err != nil { + return "", fmt.Errorf("ffmpeg not installed") + } + + cmd := exec.Command(ffmpegPath, "-version") + hideWindow(cmd) + output, err := cmd.Output() + if err == nil { + version := normalizeFFmpegVersionOutput(string(output)) + if version != "" { + return version, nil + } + } + + metadata, metadataErr := readFFmpegVersionMetadata() + if metadataErr == nil { + version := normalizeFFmpegVersion(metadata.Version) + if version != "" { + return version, nil + } + } + if err != nil { - return fmt.Errorf("failed to open tar.xz: %v", err) + return "", fmt.Errorf("failed to determine ffmpeg version: %v", err) } - defer file.Close() - xzReader, err := xz.NewReader(file) + return "", fmt.Errorf("failed to determine ffmpeg version") +} + +func normalizeFFmpegVersionOutput(output string) string { + line := strings.TrimSpace(output) + if line == "" { + return "" + } + + line = strings.Split(line, "\n")[0] + line = strings.TrimSpace(line) + lowerLine := strings.ToLower(line) + const prefix = "ffmpeg version " + if strings.HasPrefix(lowerLine, prefix) { + line = strings.TrimSpace(line[len(prefix):]) + } + + fields := strings.Fields(line) + if len(fields) > 0 { + line = fields[0] + } + + return normalizeFFmpegVersion(line) +} + +func getFFmpegVersionMetadataPath() string { + return filepath.Join(filepath.Dir(GetFFmpegPath()), "ffmpeg-version.json") +} + +func readFFmpegVersionMetadata() (ffmpegVersionMetadata, error) { + data, err := os.ReadFile(getFFmpegVersionMetadataPath()) if err != nil { - return fmt.Errorf("failed to create xz reader: %v", err) + return ffmpegVersionMetadata{}, err } - tarReader := tar.NewReader(xzReader) + var metadata ffmpegVersionMetadata + if err := json.Unmarshal(data, &metadata); err != nil { + return ffmpegVersionMetadata{}, err + } - for { - header, err := tarReader.Next() - if err == io.EOF { - break - } - if err != nil { - return fmt.Errorf("failed to read tar: %v", err) - } + return metadata, nil +} - name := filepath.Base(header.Name) - if name == "ffmpeg" && header.Typeflag == tar.TypeReg { - out, err := os.Create(destPath) - if err != nil { - return fmt.Errorf("failed to create output file: %v", err) - } - defer out.Close() +func writeFFmpegVersionMetadata(metadata ffmpegVersionMetadata) error { + metadata.Version = normalizeFFmpegVersion(metadata.Version) + if metadata.Version == "" { + return nil + } - if _, err := io.Copy(out, tarReader); err != nil { - return fmt.Errorf("failed to extract file: %v", err) - } + path := getFFmpegVersionMetadataPath() + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return fmt.Errorf("failed to create ffmpeg metadata directory: %v", err) + } - os.Chmod(destPath, 0755) - return nil + data, err := json.Marshal(metadata) + if err != nil { + return fmt.Errorf("failed to encode ffmpeg metadata: %v", err) + } + + if err := os.WriteFile(path, data, 0644); err != nil { + return fmt.Errorf("failed to write ffmpeg metadata: %v", err) + } + + return nil +} + +func normalizeFFmpegVersion(version string) string { + version = strings.TrimSpace(version) + if version == "" { + return "" + } + + lowerVersion := strings.ToLower(version) + if strings.HasPrefix(lowerVersion, "ffmpeg version ") { + version = strings.TrimSpace(version[len("ffmpeg version "):]) + } + + fields := strings.Fields(version) + if len(fields) > 0 { + version = fields[0] + } + + if strings.HasPrefix(strings.ToLower(version), "n") && len(version) > 1 { + next := version[1] + if next >= '0' && next <= '9' { + version = version[1:] } } - return fmt.Errorf("ffmpeg binary not found in archive") + if strings.HasPrefix(strings.ToLower(version), "v") { + version = strings.TrimPrefix(strings.TrimPrefix(version, "v"), "V") + } + + if version == "" { + return "" + } + + if numericVersion := ffmpegVersionPattern.FindString(version); numericVersion != "" { + version = numericVersion + } + + first := version[0] + if first >= '0' && first <= '9' { + return "v" + version + } + + return version } func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error { @@ -205,7 +540,6 @@ func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error { var args []string if quality == "fast" { - var scaleFilter string switch resolution { case "high": @@ -235,7 +569,6 @@ func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error { } } } else { - var filter string switch resolution { diff --git a/backend/folder.go b/backend/folder.go index f8608d6..2bf4a01 100644 --- a/backend/folder.go +++ b/backend/folder.go @@ -61,6 +61,25 @@ func CheckFolderExists(basePath, username string) bool { return info.IsDir() } +func CheckFoldersExist(basePath string, usernames []string) map[string]bool { + results := make(map[string]bool, len(usernames)) + if basePath == "" { + return results + } + + checked := make(map[string]bool, len(usernames)) + for _, username := range usernames { + if _, alreadyChecked := checked[username]; alreadyChecked { + continue + } + exists := CheckFolderExists(basePath, username) + checked[username] = exists + results[username] = exists + } + + return results +} + func CheckGifsFolderExists(basePath, username string) bool { gifsPath := filepath.Join(basePath, username, "gifs") info, err := os.Stat(gifsPath) diff --git a/backend/twitter.go b/backend/twitter.go index aa2a352..64f53b3 100644 --- a/backend/twitter.go +++ b/backend/twitter.go @@ -1,6 +1,7 @@ package backend import ( + "bytes" "encoding/json" "fmt" "net/url" @@ -9,8 +10,53 @@ import ( "path/filepath" "runtime" "strings" + "sync" ) +var activeExtractorCommands = struct { + sync.Mutex + byPID map[int]*exec.Cmd +}{ + byPID: make(map[int]*exec.Cmd), +} + +func registerExtractorCommand(cmd *exec.Cmd) { + if cmd == nil || cmd.Process == nil { + return + } + + activeExtractorCommands.Lock() + activeExtractorCommands.byPID[cmd.Process.Pid] = cmd + activeExtractorCommands.Unlock() +} + +func unregisterExtractorCommand(cmd *exec.Cmd) { + if cmd == nil || cmd.Process == nil { + return + } + + activeExtractorCommands.Lock() + delete(activeExtractorCommands.byPID, cmd.Process.Pid) + activeExtractorCommands.Unlock() +} + +func runTrackedExtractorCommand(cmd *exec.Cmd) ([]byte, error) { + var output bytes.Buffer + cmd.Stdout = &output + cmd.Stderr = &output + + hideWindow(cmd) + if err := cmd.Start(); err != nil { + return nil, err + } + + registerExtractorCommand(cmd) + defer unregisterExtractorCommand(cmd) + + err := cmd.Wait() + return output.Bytes(), err +} + func getExecutableName() string { if runtime.GOOS == "windows" { return "xtractor.exe" @@ -26,26 +72,18 @@ func getLegacyExecutableName() string { } func KillAllExtractorProcesses() { - names := []string{getExecutableName(), getLegacyExecutableName()} - seen := make(map[string]bool) + activeExtractorCommands.Lock() + commands := make([]*exec.Cmd, 0, len(activeExtractorCommands.byPID)) + for _, cmd := range activeExtractorCommands.byPID { + commands = append(commands, cmd) + } + activeExtractorCommands.byPID = make(map[int]*exec.Cmd) + activeExtractorCommands.Unlock() - for _, exeName := range names { - if seen[exeName] { - continue + for _, cmd := range commands { + if cmd != nil && cmd.Process != nil { + _ = cmd.Process.Kill() } - seen[exeName] = true - - var cmd *exec.Cmd - if runtime.GOOS == "windows" { - - cmd = exec.Command("taskkill", "/F", "/IM", exeName) - } else { - - cmd = exec.Command("pkill", "-f", exeName) - } - - hideWindow(cmd) - cmd.CombinedOutput() } } @@ -506,12 +544,7 @@ func ExtractTimeline(req TimelineRequest) (*TwitterResponse, error) { "PYTHONIOENCODING=utf-8", "PYTHONUTF8=1", ) - hideWindow(cmd) - output, err := cmd.CombinedOutput() - - if cmd.Process != nil { - cmd.Process.Kill() - } + output, err := runTrackedExtractorCommand(cmd) if err != nil { outputStr := string(output) @@ -533,7 +566,7 @@ func ExtractTimeline(req TimelineRequest) (*TwitterResponse, error) { return nil, fmt.Errorf("json_error: Failed to parse JSON response: %v", err) } - var timeline []TimelineEntry + timeline := make([]TimelineEntry, 0) accountInfo := AccountInfo{ Name: req.Username, Nick: req.Username, @@ -556,7 +589,6 @@ func ExtractTimeline(req TimelineRequest) (*TwitterResponse, error) { if isTextOnly { - timeline = make([]TimelineEntry, 0) for _, meta := range cliResponse.Metadata { if !mediaTweetIDs[int64(meta.TweetID)] { timeline = append(timeline, convertMetadataToTimelineEntry(meta)) @@ -695,12 +727,7 @@ func ExtractDateRange(req DateRangeRequest) (*TwitterResponse, error) { "PYTHONIOENCODING=utf-8", "PYTHONUTF8=1", ) - hideWindow(cmd) - output, err := cmd.CombinedOutput() - - if cmd.Process != nil { - cmd.Process.Kill() - } + output, err := runTrackedExtractorCommand(cmd) if err != nil { outputStr := string(output) diff --git a/frontend/eslint.config.js b/frontend/eslint.config.js index 13d2e6b..b798c52 100644 --- a/frontend/eslint.config.js +++ b/frontend/eslint.config.js @@ -5,7 +5,7 @@ import reactRefresh from 'eslint-plugin-react-refresh'; import tseslint from 'typescript-eslint'; import { defineConfig, globalIgnores } from 'eslint/config'; export default defineConfig([ - globalIgnores(['dist']), + globalIgnores(['dist', 'wailsjs']), { files: ['**/*.{ts,tsx}'], extends: [ @@ -19,4 +19,10 @@ export default defineConfig([ globals: globals.browser, }, }, + { + files: ['src/components/ui/badge.tsx', 'src/components/ui/button.tsx'], + rules: { + 'react-refresh/only-export-components': 'off', + }, + }, ]); diff --git a/frontend/index.html b/frontend/index.html index 185ac22..3dd9aeb 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -6,7 +6,7 @@ - + Twitter/X Media Batch Downloader diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 0ce4e9e..7edf16c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,10 +1,13 @@ import { useState, useEffect, useRef, useCallback } from "react"; import { TooltipProvider } from "@/components/ui/tooltip"; +import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; import { getSettings, applyThemeMode, applyFont } from "@/lib/settings"; import { applyTheme } from "@/lib/themes"; +import { compareVersionNumbers } from "@/lib/version"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; import { logger } from "@/lib/logger"; -import { saveFetchState, getFetchState, clearFetchState, getResumableInfo, stateToResponse, mergeTimelines, saveCursor, getCursor, clearCursor, type FetchState, } from "@/lib/fetch-state"; +import { saveFetchState, getFetchState, clearFetchState, getResumableInfo, mergeTimelines, saveCursor, getCursor, clearCursor, type FetchState, } from "@/lib/fetch-state"; import { TitleBar } from "@/components/TitleBar"; import { Sidebar, type PageType } from "@/components/Sidebar"; import { Header } from "@/components/Header"; @@ -13,10 +16,11 @@ import { MediaList } from "@/components/MediaList"; import { DatabaseView } from "@/components/DatabaseView"; import { SettingsPage } from "@/components/SettingsPage"; import { DebugLoggerPage } from "@/components/DebugLoggerPage"; +import { SupportPage } from "@/components/SupportPage"; import { DependencySetupDialog } from "@/components/DependencySetupDialog"; import type { HistoryItem } from "@/components/FetchHistory"; import type { TwitterResponse } from "@/types/api"; -import { ExtractTimeline, ExtractDateRange, SaveAccountToDBWithStatus, CleanupExtractorProcesses, GetAllAccountsFromDB } from "../wailsjs/go/main/App"; +import { ExtractTimeline, ExtractDateRange, SaveAccountToDBWithStatus, CleanupExtractorProcesses, GetAllAccountsFromDB, GetSavedAccountFromDB } from "../wailsjs/go/main/App"; const HISTORY_KEY = "twitter_media_fetch_history"; const MAX_HISTORY = 10; const CURRENT_VERSION = __APP_VERSION__; @@ -24,6 +28,28 @@ const BATCH_SIZE = 200; function formatNumberWithComma(num: number): string { return num.toLocaleString(); } + +function normalizeTimeline(timeline: TwitterResponse["timeline"] | null | undefined): TwitterResponse["timeline"] { + return Array.isArray(timeline) ? timeline : []; +} + +function normalizeTwitterResponse(data: TwitterResponse): TwitterResponse { + return { + ...data, + timeline: normalizeTimeline(data.timeline), + }; +} + +async function loadSavedAccountResponse(username: string, mediaType: string): Promise { + try { + const savedResponseJSON = await GetSavedAccountFromDB(username, mediaType); + return normalizeTwitterResponse(JSON.parse(savedResponseJSON) as TwitterResponse); + } + catch { + return null; + } +} + function App() { const [currentPage, setCurrentPage] = useState("main"); const [username, setUsername] = useState(""); @@ -48,6 +74,10 @@ function App() { const [isFetchingAll, setIsFetchingAll] = useState(false); const [searchMode, setSearchMode] = useState("public"); const [searchPrivateType, setSearchPrivateType] = useState("bookmarks"); + const [hasUnsavedSettings, setHasUnsavedSettings] = useState(false); + const [pendingPageChange, setPendingPageChange] = useState(null); + const [showUnsavedChangesDialog, setShowUnsavedChangesDialog] = useState(false); + const [resetSettingsFn, setResetSettingsFn] = useState<(() => void) | null>(null); const stopAllRef = useRef(false); const accountTimersRef = useRef>(new Map()); const accountStartTimesRef = useRef>(new Map()); @@ -83,7 +113,7 @@ function App() { const settings = getSettings(); applyThemeMode(settings.themeMode); applyTheme(settings.theme); - applyFont(settings.fontFamily); + applyFont(settings.fontFamily, settings.customFonts); const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); const handleChange = () => { const currentSettings = getSettings(); @@ -107,7 +137,7 @@ function App() { if (data.published_at) { setReleaseDate(data.published_at); } - if (latestVersion && latestVersion > CURRENT_VERSION) { + if (latestVersion && compareVersionNumbers(latestVersion, CURRENT_VERSION) > 0) { setHasUpdate(true); } } @@ -188,7 +218,8 @@ function App() { try { await CleanupExtractorProcesses(); } - catch { + catch (cleanupError) { + console.error("Failed to stop extractor processes:", cleanupError); } }, []); useEffect(() => { @@ -254,7 +285,8 @@ function App() { try { await CleanupExtractorProcesses(); } - catch { + catch (cleanupError) { + console.error("Failed to clean extractor processes before fetch:", cleanupError); } const fetchTarget = isBookmarks ? "your bookmarks" @@ -262,6 +294,7 @@ function App() { ? "your likes" : `@${username}`; const cleanUsername = isBookmarks ? "bookmarks" : isLikes ? "likes" : username.trim(); + const requestedMediaType = mediaType || "all"; let existingState: FetchState | null = null; let cursor: string | undefined; let allTimeline: TwitterResponse["timeline"] = []; @@ -270,17 +303,18 @@ function App() { existingState = getFetchState(cleanUsername); if (existingState && existingState.cursor && !existingState.completed) { cursor = existingState.cursor; - allTimeline = existingState.timeline; - accountInfo = existingState.accountInfo; - logger.info(`Resuming ${fetchTarget} from ${allTimeline.length} items...`); - if (accountInfo) { - setResult({ - account_info: accountInfo, - timeline: allTimeline, - total_urls: allTimeline.length, - metadata: { new_entries: 0, page: 0, batch_size: 0, has_more: true }, - }); + const savedResponse = await loadSavedAccountResponse(cleanUsername, existingState.mediaType || requestedMediaType); + if (!savedResponse) { + clearFetchState(cleanUsername); + clearCursor(cleanUsername); + toast.error("Resume data is no longer available"); + setLoading(false); + return; } + allTimeline = savedResponse.timeline; + accountInfo = savedResponse.account_info; + logger.info(`Resuming ${fetchTarget} from ${existingState.totalFetched} items...`); + setResult(savedResponse); } else { toast.error("No resumable fetch found"); @@ -314,21 +348,13 @@ function App() { auth_token: authToken.trim(), start_date: startDate, end_date: endDate, - media_filter: mediaType || "all", + media_filter: requestedMediaType, retweets: retweets || false, }); - finalData = JSON.parse(response); + finalData = normalizeTwitterResponse(JSON.parse(response) as TwitterResponse); if (finalData && finalData.account_info) { try { - await SaveAccountToDBWithStatus(finalData.account_info.name, finalData.account_info.nick, finalData.account_info.profile_image, finalData.total_urls, JSON.stringify(finalData), mediaType || "all", finalData.cursor || "", finalData.completed ?? true); - } - catch (err) { - reportDatabaseSaveError("date range data", err); - } - } - if (finalData && finalData.account_info) { - try { - await SaveAccountToDBWithStatus(finalData.account_info.name, finalData.account_info.nick, finalData.account_info.profile_image, finalData.total_urls, JSON.stringify(finalData), mediaType || "all", finalData.cursor || "", finalData.completed ?? true); + await SaveAccountToDBWithStatus(finalData.account_info.name, finalData.account_info.nick, finalData.account_info.profile_image, finalData.total_urls, JSON.stringify(finalData), requestedMediaType, finalData.cursor || "", finalData.completed ?? true); } catch (err) { reportDatabaseSaveError("date range data", err); @@ -361,12 +387,9 @@ function App() { saveFetchState({ username: cleanUsername, cursor: cursor || "", - timeline: allTimeline, - accountInfo: accountInfo, totalFetched: allTimeline.length, completed: false, - authToken: authToken.trim(), - mediaType: mediaType || "all", + mediaType: requestedMediaType, retweets: retweets || false, timelineType: timelineType, }); @@ -386,7 +409,7 @@ function App() { completed: false, }; try { - await SaveAccountToDBWithStatus(accountInfo.name, accountInfo.nick, accountInfo.profile_image, allTimeline.length, JSON.stringify(timeoutResponse), mediaType || "all", cursor || "", false); + await SaveAccountToDBWithStatus(accountInfo.name, accountInfo.nick, accountInfo.profile_image, allTimeline.length, JSON.stringify(timeoutResponse), requestedMediaType, cursor || "", false); logger.info(`Saved ${allTimeline.length} items before timeout`); } catch (err) { @@ -404,11 +427,11 @@ function App() { timeline_type: timelineType, batch_size: batchSize, page: page, - media_type: mediaType || "all", + media_type: requestedMediaType, retweets: retweets || false, cursor: cursor, }); - const data: TwitterResponse = JSON.parse(response); + const data = normalizeTwitterResponse(JSON.parse(response) as TwitterResponse); if (!accountInfo && data.account_info) { accountInfo = data.account_info; if (isBookmarks) { @@ -454,12 +477,9 @@ function App() { saveFetchState({ username: cleanUsername, cursor: cursor || "", - timeline: allTimeline, - accountInfo: accountInfo, totalFetched: allTimeline.length, completed: !hasMore, - authToken: authToken.trim(), - mediaType: mediaType || "all", + mediaType: requestedMediaType, retweets: retweets || false, timelineType: timelineType, }); @@ -480,7 +500,7 @@ function App() { completed: !hasMore, }; try { - await SaveAccountToDBWithStatus(accountInfo.name, accountInfo.nick, accountInfo.profile_image, allTimeline.length, JSON.stringify(currentResponse), mediaType || "all", cursor || "", !hasMore); + await SaveAccountToDBWithStatus(accountInfo.name, accountInfo.nick, accountInfo.profile_image, allTimeline.length, JSON.stringify(currentResponse), requestedMediaType, cursor || "", !hasMore); } catch (err) { reportDatabaseSaveError("fetch progress", err); @@ -523,7 +543,7 @@ function App() { } if (!useDateRange) { try { - await SaveAccountToDBWithStatus(finalData.account_info.name, finalData.account_info.nick, finalData.account_info.profile_image, finalData.total_urls, JSON.stringify(finalData), mediaType || "all", finalData.cursor || "", finalData.completed ?? true); + await SaveAccountToDBWithStatus(finalData.account_info.name, finalData.account_info.nick, finalData.account_info.profile_image, finalData.total_urls, JSON.stringify(finalData), requestedMediaType, finalData.cursor || "", finalData.completed ?? true); } catch (err) { reportDatabaseSaveError("final fetch status", err); @@ -547,14 +567,14 @@ function App() { toast.error("Failed to fetch media"); } const savedState = getFetchState(cleanUsername); - if (savedState && savedState.timeline.length > 0) { - const partialResponse = stateToResponse(savedState); + if (savedState && savedState.totalFetched > 0) { + const partialResponse = await loadSavedAccountResponse(cleanUsername, savedState.mediaType || requestedMediaType); if (partialResponse) { setResult(partialResponse); - setResumeInfo({ canResume: true, mediaCount: savedState.timeline.length }); - toast.info(`Saved ${formatNumberWithComma(savedState.timeline.length)} items - can resume`); + setResumeInfo({ canResume: true, mediaCount: savedState.totalFetched }); + toast.info(`Saved ${formatNumberWithComma(savedState.totalFetched)} items - can resume`); try { - await SaveAccountToDBWithStatus(partialResponse.account_info.name, partialResponse.account_info.nick, partialResponse.account_info.profile_image, partialResponse.total_urls, JSON.stringify(partialResponse), mediaType || "all", savedState.cursor || "", false); + await SaveAccountToDBWithStatus(partialResponse.account_info.name, partialResponse.account_info.nick, partialResponse.account_info.profile_image, partialResponse.total_urls, JSON.stringify(partialResponse), savedState.mediaType || requestedMediaType, savedState.cursor || "", false); } catch (dbErr) { reportDatabaseSaveError("partial fetch data", dbErr); @@ -575,13 +595,14 @@ function App() { const handleClearResume = () => { if (username.trim()) { clearFetchState(username.trim()); + clearCursor(username.trim()); setResumeInfo(null); toast.info("Resume data cleared"); } }; const handleLoadFromDB = (responseJSON: string, loadedUsername: string) => { try { - const data: TwitterResponse = JSON.parse(responseJSON); + const data = normalizeTwitterResponse(JSON.parse(responseJSON) as TwitterResponse); setResult(data); setUsername(loadedUsername); setCurrentPage("main"); @@ -603,6 +624,7 @@ function App() { toast.success(`Loaded @${loadedUsername} from database`); } catch (error) { + console.error("Failed to parse saved data:", error); toast.error("Failed to parse saved data"); } }; @@ -677,26 +699,27 @@ function App() { toast.success(`Imported ${formatNumberWithComma(accounts.length)} account(s)`); } catch (error) { + console.error("Failed to read imported account list:", error); toast.error("Failed to read file"); } }; input.click(); }; - const handleFetchAll = async () => { + const handleFetchAll = async (authToken: string) => { if (multipleAccounts.length === 0) { toast.error("No accounts to fetch"); return; } setIsFetchingAll(true); stopAllRef.current = false; - const PUBLIC_AUTH_TOKEN_KEY = "twitter_public_auth_token"; - const authToken = localStorage.getItem(PUBLIC_AUTH_TOKEN_KEY) || ""; if (!authToken.trim()) { toast.error("Please enter your auth token"); setIsFetchingAll(false); return; } const settings = getSettings(); + const selectedMediaType = settings.mediaType || "all"; + setFetchedMediaType(selectedMediaType); const timeoutSeconds = settings.fetchTimeout || 60; setMultipleAccounts((prev) => prev.map((acc) => ({ ...acc, @@ -708,7 +731,7 @@ function App() { error: undefined, showDiff: false, }))); - let currentAccounts = [...multipleAccounts]; + const currentAccounts = [...multipleAccounts]; for (let i = 0; i < currentAccounts.length; i++) { if (stopAllRef.current) { break; @@ -785,12 +808,9 @@ function App() { saveFetchState({ username: cleanUsername, cursor: cursor || "", - timeline: allTimeline, - accountInfo: accountInfo, totalFetched: allTimeline.length, completed: false, - authToken: authToken.trim(), - mediaType: fetchedMediaType || "all", + mediaType: selectedMediaType, retweets: false, timelineType: "timeline", }); @@ -809,7 +829,7 @@ function App() { }, cursor: cursor, completed: false, - }), fetchedMediaType || "all", cursor || "", false); + }), selectedMediaType, cursor || "", false); } catch (err) { reportDatabaseSaveError("multi-account timeout state", err); @@ -823,11 +843,11 @@ function App() { timeline_type: "timeline", batch_size: batchSizeMultiple, page: page, - media_type: fetchedMediaType || "all", + media_type: selectedMediaType, retweets: false, cursor: cursor, }); - const data: TwitterResponse = JSON.parse(response); + const data = normalizeTwitterResponse(JSON.parse(response) as TwitterResponse); if (!accountInfo && data.account_info) { accountInfo = data.account_info; } @@ -864,12 +884,9 @@ function App() { saveFetchState({ username: cleanUsername, cursor: cursor || "", - timeline: allTimeline, - accountInfo: accountInfo, totalFetched: allTimeline.length, completed: !hasMore, - authToken: authToken.trim(), - mediaType: fetchedMediaType || "all", + mediaType: selectedMediaType, retweets: false, timelineType: "timeline", }); @@ -890,7 +907,7 @@ function App() { }, cursor: cursor, completed: !hasMore, - }), fetchedMediaType || "all", cursor || "", !hasMore); + }), selectedMediaType, cursor || "", !hasMore); } catch (err) { reportDatabaseSaveError("multi-account progress", err); @@ -1004,17 +1021,44 @@ function App() { CleanupExtractorProcesses().catch(() => { }); toast.info("Stopped account fetch"); }; - const handleRetryAccount = async (accountId: string) => { + const handlePageChange = (page: PageType) => { + if (currentPage === "settings" && hasUnsavedSettings && page !== "settings") { + setPendingPageChange(page); + setShowUnsavedChangesDialog(true); + return; + } + setCurrentPage(page); + }; + const handleDiscardChanges = () => { + setShowUnsavedChangesDialog(false); + if (resetSettingsFn) { + resetSettingsFn(); + } + const savedSettings = getSettings(); + applyThemeMode(savedSettings.themeMode); + applyTheme(savedSettings.theme); + applyFont(savedSettings.fontFamily, savedSettings.customFonts); + setHasUnsavedSettings(false); + if (pendingPageChange) { + setCurrentPage(pendingPageChange); + setPendingPageChange(null); + } + }; + const handleCancelNavigation = () => { + setShowUnsavedChangesDialog(false); + setPendingPageChange(null); + }; + const handleRetryAccount = async (accountId: string, authToken: string) => { const account = multipleAccounts.find((acc) => acc.id === accountId); if (!account) return; - const PUBLIC_AUTH_TOKEN_KEY = "twitter_public_auth_token"; - const authToken = localStorage.getItem(PUBLIC_AUTH_TOKEN_KEY) || ""; if (!authToken.trim()) { toast.error("Please enter your auth token"); return; } const retrySettings = getSettings(); + const selectedMediaType = retrySettings.mediaType || fetchedMediaType || "all"; + setFetchedMediaType(selectedMediaType); const isSingleModeRetry = retrySettings.fetchMode === "single"; const batchSizeRetry = isSingleModeRetry ? 0 : BATCH_SIZE; const cleanUsername = account.username.trim(); @@ -1026,12 +1070,22 @@ function App() { existingState = getFetchState(cleanUsername); if (existingState && existingState.cursor && !existingState.completed) { cursor = existingState.cursor; - allTimeline = existingState.timeline; - if (existingState.accountInfo) { - accountInfo = existingState.accountInfo; + const savedResponse = await loadSavedAccountResponse(cleanUsername, existingState.mediaType || selectedMediaType); + if (savedResponse) { + allTimeline = savedResponse.timeline; + accountInfo = savedResponse.account_info; + previousMediaCount = existingState.totalFetched; + logger.info(`Resuming @${cleanUsername} from ${existingState.totalFetched} items...`); + } + else { + clearFetchState(cleanUsername); + clearCursor(cleanUsername); + logger.info(`Saved retry state for @${cleanUsername} was missing. Starting from beginning...`); + cursor = undefined; + allTimeline = []; + accountInfo = null; + previousMediaCount = 0; } - previousMediaCount = existingState.timeline.length; - logger.info(`Resuming @${cleanUsername} from ${allTimeline.length} items...`); } else { if (!cursor) { @@ -1044,7 +1098,7 @@ function App() { else { try { const accounts = await GetAllAccountsFromDB(); - const dbAccount = accounts.find((acc) => acc.username.toLowerCase() === cleanUsername.toLowerCase()); + const dbAccount = accounts.find((acc) => acc.username.toLowerCase() === cleanUsername.toLowerCase() && (acc.media_type || "all") === selectedMediaType); if (dbAccount?.cursor) { cursor = dbAccount.cursor; previousMediaCount = account.mediaCount || 0; @@ -1130,11 +1184,11 @@ function App() { timeline_type: "timeline", batch_size: batchSizeRetry, page: page, - media_type: fetchedMediaType || "all", + media_type: selectedMediaType, retweets: false, cursor: cursor, }); - const data: TwitterResponse = JSON.parse(response); + const data = normalizeTwitterResponse(JSON.parse(response) as TwitterResponse); if (!accountInfo && data.account_info) { accountInfo = data.account_info; } @@ -1165,12 +1219,9 @@ function App() { saveFetchState({ username: cleanUsername, cursor: cursor || "", - timeline: allTimeline, - accountInfo: accountInfo, totalFetched: allTimeline.length, completed: !hasMore, - authToken: authToken.trim(), - mediaType: fetchedMediaType || "all", + mediaType: selectedMediaType, retweets: false, timelineType: "timeline", }); @@ -1197,7 +1248,7 @@ function App() { }, cursor: cursor, completed: !hasMore, - }), fetchedMediaType || "all", cursor || "", !hasMore); + }), selectedMediaType, cursor || "", !hasMore); } catch (err) { reportDatabaseSaveError("retry progress", err); @@ -1271,11 +1322,13 @@ function App() { const renderPage = () => { switch (currentPage) { case "settings": - return ; + return ; case "debug": return ; case "database": return ( setCurrentPage("main")} onLoadAccount={handleLoadFromDB} onUpdateSelected={handleUpdateSelected}/>); + case "support": + return ; default: return (<>
@@ -1294,17 +1347,43 @@ function App() { } }; return ( -
+
- - - -
-
- {renderPage()} + + +
+
+
+ {renderPage()} +
+ + { + if (open) { + setShowUnsavedChangesDialog(true); + return; + } + handleCancelNavigation(); + }}> + + + Unsaved Changes + + You have unsaved changes in Settings. If you leave now, your changes will be lost. + + + + + + + +
); } diff --git a/frontend/src/assets/ko-fi.gif b/frontend/src/assets/ko-fi.gif new file mode 100644 index 0000000..221d0b9 Binary files /dev/null and b/frontend/src/assets/ko-fi.gif differ diff --git a/frontend/src/assets/kofi_symbol.svg b/frontend/src/assets/kofi_symbol.svg new file mode 100644 index 0000000..ade749d --- /dev/null +++ b/frontend/src/assets/kofi_symbol.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/frontend/src/assets/usdt.jpg b/frontend/src/assets/usdt.jpg new file mode 100644 index 0000000..5732bda Binary files /dev/null and b/frontend/src/assets/usdt.jpg differ diff --git a/frontend/src/components/DatabaseView.tsx b/frontend/src/components/DatabaseView.tsx index 3524837..979c03f 100644 --- a/frontend/src/components/DatabaseView.tsx +++ b/frontend/src/components/DatabaseView.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef } from "react"; +import { useState, useEffect, useRef, useCallback, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; @@ -15,7 +15,7 @@ import { Trash2, FileInput, FileOutput, Pencil, Tag, Shuffle, X, XCircle, Downlo import { toastWithSound as toast } from "@/lib/toast-with-sound"; import { getSettings } from "@/lib/settings"; import { openExternal } from "@/lib/utils"; -import { GetAllAccountsFromDB, GetAccountFromDB, DeleteAccountFromDB, SaveAccountToDB, ExportAccountJSON, ExportAccountsTXT, UpdateAccountGroup, GetAllGroups, DownloadMediaWithMetadata, StopDownload, CheckFolderExists, OpenFolder, GetFolderPath, } from "../../wailsjs/go/main/App"; +import { GetAllAccountsFromDB, GetAccountFromDB, DeleteAccountFromDB, SaveAccountToDB, ExportAccountJSON, ExportAccountsTXT, UpdateAccountGroup, GetAllGroups, DownloadMediaWithMetadata, StopDownload, CheckFoldersExist, OpenFolder, GetFolderPath, } from "../../wailsjs/go/main/App"; import { EventsOn, EventsOff } from "../../wailsjs/runtime/runtime"; import { main } from "../../wailsjs/go/models"; interface DownloadProgress { @@ -66,6 +66,11 @@ interface DatabaseViewProps { } const INITIAL_LOAD_COUNT = 12; const LOAD_MORE_COUNT = 12; + +function getContentScrollElement(): HTMLElement | null { + return document.getElementById("app-content-scroll"); +} + export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewProps) { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(true); @@ -92,26 +97,16 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr const stopBulkDownloadRef = useRef(false); const loadMoreRef = useRef(null); const [folderExistence, setFolderExistence] = useState>(new Map()); - const checkFolderExistence = async (accountsList: AccountListItem[]) => { - const settings = getSettings(); - const basePath = settings.downloadPath; - if (!basePath) - return; - const folderMap = new Map(); - for (const account of accountsList) { - let folderName = account.username; - if (account.username === "bookmarks") { - folderName = "My Bookmarks"; - } - else if (account.username === "likes") { - folderName = "My Likes"; - } - const exists = await CheckFolderExists(basePath, folderName); - folderMap.set(account.id, exists); + const getFolderNameForAccount = (username: string) => { + if (username === "bookmarks") { + return "My Bookmarks"; } - setFolderExistence(folderMap); + if (username === "likes") { + return "My Likes"; + } + return username; }; - const loadAccounts = async () => { + const loadAccounts = useCallback(async () => { setLoading(true); try { const data = await GetAllAccountsFromDB(); @@ -120,9 +115,6 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr if (groupsData) { setGroups(groupsData.map((g) => ({ name: g.name || "", color: g.color || "" }))); } - if (data && data.length > 0) { - checkFolderExistence(data); - } } catch (error) { console.error("Failed to load accounts:", error); @@ -131,19 +123,24 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr finally { setLoading(false); } - }; - useEffect(() => { - loadAccounts(); }, []); + useEffect(() => { + void loadAccounts(); + }, [loadAccounts]); useEffect(() => { setVisibleCount(INITIAL_LOAD_COUNT); }, [gridView, searchQuery, filterGroup, filterMediaType, accountViewMode, sortOrder]); useEffect(() => { + const scrollElement = getContentScrollElement(); + if (!scrollElement) { + return; + } const handleScroll = () => { - setShowScrollTop(window.scrollY > 300); + setShowScrollTop(scrollElement.scrollTop > 300); }; - window.addEventListener("scroll", handleScroll); - return () => window.removeEventListener("scroll", handleScroll); + handleScroll(); + scrollElement.addEventListener("scroll", handleScroll); + return () => scrollElement.removeEventListener("scroll", handleScroll); }, []); useEffect(() => { const unsubscribe = EventsOn("download-progress", (progress: DownloadProgress) => { @@ -155,72 +152,116 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr }; }, []); const scrollToTop = () => { - window.scrollTo({ top: 0, behavior: "smooth" }); + getContentScrollElement()?.scrollTo({ top: 0, behavior: "smooth" }); }; const isPrivateAccount = (username: string) => username === "bookmarks" || username === "likes"; - const publicAccounts = accounts.filter((acc) => !isPrivateAccount(acc.username)); - const privateAccounts = accounts.filter((acc) => isPrivateAccount(acc.username)); - const baseAccounts = accountViewMode === "public" ? publicAccounts : privateAccounts; - const filteredAccounts = baseAccounts - .filter((acc) => { - if (searchQuery) { - const query = searchQuery.toLowerCase(); - const matchesUsername = acc.username.toLowerCase().includes(query); - const matchesName = acc.name.toLowerCase().includes(query); - if (!matchesUsername && !matchesName) - return false; - } - if (filterGroup !== "all") { - if (filterGroup === "ungrouped" && acc.group_name) - return false; - if (filterGroup !== "ungrouped" && acc.group_name !== filterGroup) - return false; - } - if (filterMediaType !== "all") { - const accMediaType = acc.media_type || "all"; - if (filterMediaType === "all-media") { - if (accMediaType !== "all") + const currentDownloadPath = getSettings().downloadPath || ""; + const publicAccounts = useMemo(() => accounts.filter((acc) => !isPrivateAccount(acc.username)), [accounts]); + const privateAccounts = useMemo(() => accounts.filter((acc) => isPrivateAccount(acc.username)), [accounts]); + const filteredAccounts = useMemo(() => { + const baseAccounts = accountViewMode === "public" ? publicAccounts : privateAccounts; + return baseAccounts + .filter((acc) => { + if (searchQuery) { + const query = searchQuery.toLowerCase(); + const matchesUsername = acc.username.toLowerCase().includes(query); + const matchesName = acc.name.toLowerCase().includes(query); + if (!matchesUsername && !matchesName) return false; } - else { - if (accMediaType !== filterMediaType) + if (filterGroup !== "all") { + if (filterGroup === "ungrouped" && acc.group_name) + return false; + if (filterGroup !== "ungrouped" && acc.group_name !== filterGroup) return false; } - } - return true; - }) - .sort((a, b) => { - switch (sortOrder) { - case "newest": { - const dateA = new Date(a.last_fetched).getTime(); - const dateB = new Date(b.last_fetched).getTime(); - return dateB - dateA; + if (filterMediaType !== "all") { + const accMediaType = acc.media_type || "all"; + if (filterMediaType === "all-media") { + if (accMediaType !== "all") + return false; + } + else if (accMediaType !== filterMediaType) { + return false; + } } - case "oldest": { - const dateA = new Date(a.last_fetched).getTime(); - const dateB = new Date(b.last_fetched).getTime(); - return dateA - dateB; + return true; + }) + .sort((a, b) => { + switch (sortOrder) { + case "newest": { + const dateA = new Date(a.last_fetched).getTime(); + const dateB = new Date(b.last_fetched).getTime(); + return dateB - dateA; + } + case "oldest": { + const dateA = new Date(a.last_fetched).getTime(); + const dateB = new Date(b.last_fetched).getTime(); + return dateA - dateB; + } + case "username-asc": + return a.username.toLowerCase().localeCompare(b.username.toLowerCase()); + case "username-desc": + return b.username.toLowerCase().localeCompare(a.username.toLowerCase()); + case "followers-high": + return (b.followers_count || 0) - (a.followers_count || 0); + case "followers-low": + return (a.followers_count || 0) - (b.followers_count || 0); + case "posts-high": + return (b.statuses_count || 0) - (a.statuses_count || 0); + case "posts-low": + return (a.statuses_count || 0) - (b.statuses_count || 0); + case "media-high": + return (b.total_media || 0) - (a.total_media || 0); + case "media-low": + return (a.total_media || 0) - (b.total_media || 0); + default: + return 0; } - case "username-asc": - return a.username.toLowerCase().localeCompare(b.username.toLowerCase()); - case "username-desc": - return b.username.toLowerCase().localeCompare(a.username.toLowerCase()); - case "followers-high": - return (b.followers_count || 0) - (a.followers_count || 0); - case "followers-low": - return (a.followers_count || 0) - (b.followers_count || 0); - case "posts-high": - return (b.statuses_count || 0) - (a.statuses_count || 0); - case "posts-low": - return (a.statuses_count || 0) - (b.statuses_count || 0); - case "media-high": - return (b.total_media || 0) - (a.total_media || 0); - case "media-low": - return (a.total_media || 0) - (b.total_media || 0); - default: - return 0; + }); + }, [accountViewMode, filterGroup, filterMediaType, privateAccounts, publicAccounts, searchQuery, sortOrder]); + const visibleAccounts = useMemo(() => filteredAccounts.slice(0, visibleCount), [filteredAccounts, visibleCount]); + const visibleAccountsKey = visibleAccounts + .map((account) => `${account.id}:${account.username}`) + .join("|"); + useEffect(() => { + const basePath = currentDownloadPath; + if (!basePath) { + setFolderExistence(new Map()); + return; } - }); + + if (visibleAccounts.length === 0) { + return; + } + + let active = true; + const loadVisibleFolderExistence = async () => { + try { + const folderNames = visibleAccounts.map((account) => getFolderNameForAccount(account.username)); + const results = await CheckFoldersExist(basePath, folderNames); + if (!active) { + return; + } + + setFolderExistence((prev) => { + const next = new Map(prev); + for (const account of visibleAccounts) { + next.set(account.id, results[getFolderNameForAccount(account.username)] ?? false); + } + return next; + }); + } + catch (error) { + console.error("Failed to check visible folders:", error); + } + }; + + void loadVisibleFolderExistence(); + return () => { + active = false; + }; + }, [currentDownloadPath, visibleAccounts, visibleAccountsKey]); useEffect(() => { const currentRef = loadMoreRef.current; if (!currentRef) @@ -247,9 +288,10 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr await UpdateAccountGroup(editingAccount.id, editGroupName, editGroupColor); toast.success(`Updated group for @${editingAccount.username}`); setEditingAccount(null); - loadAccounts(); + await loadAccounts(); } catch (error) { + console.error("Failed to update group:", error); toast.error("Failed to update group"); } }; @@ -257,9 +299,10 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr try { await DeleteAccountFromDB(id); toast.success(`Deleted @${username}`); - loadAccounts(); + await loadAccounts(); } catch (error) { + console.error(`Failed to delete @${username}:`, error); toast.error("Failed to delete account"); } }; @@ -269,6 +312,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr onLoadAccount(responseJSON, username); } catch (error) { + console.error(`Failed to load @${username} from database:`, error); toast.error("Failed to load account data"); } }; @@ -286,6 +330,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr await OpenFolder(folderPath); } catch (error) { + console.error(`Failed to open folder for @${username}:`, error); toast.error("Failed to open folder"); } }; @@ -335,7 +380,13 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); - if (response.success) { + if (response.cancelled) { + const message = response.downloaded > 0 || response.skipped > 0 + ? `Download stopped for @${username} after ${response.downloaded + response.skipped} items` + : `Download stopped for @${username}`; + toast.info(message); + } + else if (response.success) { const parts: string[] = []; if (response.downloaded > 0) { parts.push(`${response.downloaded} file${response.downloaded !== 1 ? 's' : ''} downloaded`); @@ -448,6 +499,11 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); + if (response.cancelled) { + toast.info(`Stopped while downloading @${account.username}`); + stopBulkDownloadRef.current = true; + break; + } if (response.success) { totalDownloaded += response.downloaded; totalSkipped += response.skipped || 0; @@ -521,6 +577,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Exported ${exported} account(s) to ${outputDir}\\twitterxmediabatchdownloader_backups`); } catch (error) { + console.error("Failed to export JSON backups:", error); toast.error("Failed to export"); } }; @@ -537,6 +594,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Exported ${formatNumberWithComma(idsToExport.length)} account(s) to ${outputDir}\\twitterxmediabatchdownloader_backups\\twitterxmediabatchdownloader_multiple.txt`); } catch (error) { + console.error("Failed to export TXT backup:", error); toast.error("Failed to export"); } }; @@ -782,9 +840,10 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Deleted ${formatNumberWithComma(idsToDelete.length)} account${idsToDelete.length !== 1 ? 's' : ''}`); setClearAllDialogOpen(false); setSelectedIds(new Set()); - loadAccounts(); + await loadAccounts(); } catch (error) { + console.error("Failed to delete selected accounts:", error); toast.error("Failed to delete accounts"); } }}> @@ -993,6 +1052,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Exported @${account.username}`); } catch (error) { + console.error(`Failed to export @${account.username}:`, error); toast.error("Failed to export"); } }}> @@ -1081,6 +1141,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Exported @${account.username}`); } catch (error) { + console.error(`Failed to export @${account.username}:`, error); toast.error("Failed to export"); } }}> @@ -1209,6 +1270,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.success(`Exported @${account.username} to ${outputDir}\\twitterxmediabatchdownloader_backups`); } catch (error) { + console.error(`Failed to export @${account.username}:`, error); toast.error("Failed to export"); } }}> diff --git a/frontend/src/components/DebugLoggerPage.tsx b/frontend/src/components/DebugLoggerPage.tsx index f2d8853..9ff28f7 100644 --- a/frontend/src/components/DebugLoggerPage.tsx +++ b/frontend/src/components/DebugLoggerPage.tsx @@ -18,14 +18,13 @@ function formatTime(date: Date): string { }); } export function DebugLoggerPage() { - const [logs, setLogs] = useState([]); + const [logs, setLogs] = useState(() => logger.getLogs()); const [copied, setCopied] = useState(false); const scrollRef = useRef(null); useEffect(() => { const unsubscribe = logger.subscribe(() => { setLogs(logger.getLogs()); }); - setLogs(logger.getLogs()); return () => { unsubscribe(); }; diff --git a/frontend/src/components/DependencySetupDialog.tsx b/frontend/src/components/DependencySetupDialog.tsx index 589298a..f69a190 100644 --- a/frontend/src/components/DependencySetupDialog.tsx +++ b/frontend/src/components/DependencySetupDialog.tsx @@ -4,17 +4,31 @@ import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Spinner } from "@/components/ui/spinner"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; +import { Quit } from "../../wailsjs/runtime/runtime"; import { DownloadExtractor, IsExtractorInstalled } from "../../wailsjs/go/main/App"; + interface DependencySetupDialogProps { onInstalled?: () => void; } + +interface GithubReleaseResponse { + tag_name?: string; +} + +const extractorReleaseAPIURL = "https://api.github.com/repos/afkarxyz/xtractor-binaries/releases/latest"; + export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProps) { const [open, setOpen] = useState(false); const [checking, setChecking] = useState(true); const [downloading, setDownloading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); + const [releaseVersion, setReleaseVersion] = useState(null); + const [releaseStatusMessage, setReleaseStatusMessage] = useState("Checking latest GitHub release..."); + useEffect(() => { let cancelled = false; + const abortController = new AbortController(); + const checkDependency = async () => { try { const installed = await IsExtractorInstalled(); @@ -26,7 +40,7 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp catch { if (!cancelled) { setOpen(true); - setErrorMessage("Couldn't verify the core dependency. You can install it now."); + setErrorMessage("Couldn't verify Xtractor. Download it to continue."); } } finally { @@ -35,48 +49,94 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp } } }; - checkDependency(); + + const loadReleaseInfo = async () => { + try { + const response = await fetch(extractorReleaseAPIURL, { + headers: { + Accept: "application/vnd.github+json", + }, + signal: abortController.signal, + }); + + if (!response.ok) { + throw new Error(`GitHub release status ${response.status}`); + } + + const release = (await response.json()) as GithubReleaseResponse; + const version = release.tag_name?.trim(); + + if (!version) { + throw new Error("Missing GitHub release tag"); + } + + if (!cancelled) { + setReleaseVersion(version); + setReleaseStatusMessage(`Latest GitHub release: ${version}`); + } + } + catch { + if (!cancelled && !abortController.signal.aborted) { + setReleaseVersion(null); + setReleaseStatusMessage("Latest GitHub release unavailable."); + } + } + }; + + void checkDependency(); + void loadReleaseInfo(); + return () => { cancelled = true; + abortController.abort(); }; }, []); + const handleDownload = async () => { setDownloading(true); setErrorMessage(null); try { await DownloadExtractor(); setOpen(false); - toast.success("Core dependency installed"); + toast.success("Xtractor installed"); onInstalled?.(); } catch (error) { const message = error instanceof Error ? error.message : String(error); - setErrorMessage(message || "Failed to download dependency."); - toast.error("Failed to download dependency"); + setErrorMessage(message || "Failed to download Xtractor."); + toast.error("Failed to download Xtractor"); } finally { setDownloading(false); } }; + + const handleExit = () => { + if (!downloading) { + void Quit(); + } + }; + if (checking && !open) { return null; } - return ( !downloading && setOpen(nextOpen)}> - + + return ( { + if (nextOpen) { + setOpen(true); + } + }}> + event.preventDefault()} onInteractOutside={(event) => event.preventDefault()}>
- Download Core Dependency - - This app now uses a prebuilt xtractor binary from GitHub releases. Download it once so fetching can work on this device. - + Download Xtractor + Xtractor is required before you can use this app.
-
- The binary is stored in the app data folder and reused on the next launch. Optional tools like FFmpeg and ExifTool stay available in Settings. -
+

{releaseVersion ? `Latest GitHub release: ${releaseVersion}` : releaseStatusMessage}

{errorMessage && (
@@ -85,16 +145,16 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp
- diff --git a/frontend/src/components/MediaList.tsx b/frontend/src/components/MediaList.tsx index 1668e9d..59f50db 100644 --- a/frontend/src/components/MediaList.tsx +++ b/frontend/src/components/MediaList.tsx @@ -11,6 +11,7 @@ import type { TimelineEntry, AccountInfo } from "@/types/api"; import { logger } from "@/lib/logger"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; import { getSettings } from "@/lib/settings"; +import { getCachedDependencyStatus, getCachedMediaFolderStatus, setCachedDependencyStatus, setCachedMediaFolderStatus } from "@/lib/runtime-cache"; import { openExternal } from "@/lib/utils"; import { DownloadMediaWithMetadata, OpenFolder, IsFFmpegInstalled, ConvertGIFs, StopDownload, CheckFolderExists, CheckGifsFolderHasMP4 } from "../../wailsjs/go/main/App"; import { EventsOn, EventsOff } from "../../wailsjs/runtime/runtime"; @@ -32,9 +33,15 @@ interface MediaListProps { fetchedMediaType?: string; newMediaCount?: number | null; } +const MEDIA_LIST_PAGE_SIZE = 30; + +function getContentScrollElement(): HTMLElement | null { + return document.getElementById("app-content-scroll"); +} + function getThumbnailUrl(url: string): string { if (url.includes("video.twimg.com/tweet_video/")) { - const match = url.match(/tweet_video\/([^\/]+)\.mp4/); + const match = url.match(/tweet_video\/([^/]+)\.mp4/); if (match && match[1]) { const filename = match[1]; return `https://pbs.twimg.com/tweet_video_thumb/${filename}?format=jpg&name=360x360`; @@ -161,7 +168,23 @@ function formatJoinDate(dateStr: string): string { return dateStr; } } + +function getAccountFolderName(accountInfo: AccountInfo): string { + if (accountInfo.nick === "My Bookmarks" || accountInfo.name === "bookmarks") { + return "My Bookmarks"; + } + if (accountInfo.nick === "My Likes" || accountInfo.name === "likes") { + return "My Likes"; + } + return accountInfo.name; +} + export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = "all", newMediaCount = null, }: MediaListProps) { + const settings = getSettings(); + const folderName = getAccountFolderName(accountInfo); + const cachedMediaFolderStatus = settings.downloadPath + ? getCachedMediaFolderStatus(settings.downloadPath, folderName) + : null; const [selectedItems, setSelectedItems] = useState>(new Set()); const [sortBy, setSortBy] = useState("date-desc"); const [filterType, setFilterType] = useState("all"); @@ -170,23 +193,28 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = const [downloadProgress, setDownloadProgress] = useState(null); const [hasDownloaded, setHasDownloaded] = useState(false); const [isConverting, setIsConverting] = useState(false); - const [ffmpegInstalled, setFfmpegInstalled] = useState(false); + const [ffmpegInstalled, setFfmpegInstalled] = useState(() => getCachedDependencyStatus("ffmpeg").installed ?? false); const [showScrollTop, setShowScrollTop] = useState(false); - const [folderExists, setFolderExists] = useState(false); - const [gifsFolderHasMP4, setGifsFolderHasMP4] = useState(false); + const [folderExists, setFolderExists] = useState(cachedMediaFolderStatus?.folderExists ?? false); + const [gifsFolderHasMP4, setGifsFolderHasMP4] = useState(cachedMediaFolderStatus?.gifsFolderHasMP4 ?? false); const [previewIndex, setPreviewIndex] = useState(null); const [downloadedItems, setDownloadedItems] = useState>(new Set()); const [failedItems, setFailedItems] = useState>(new Set()); const [skippedItems, setSkippedItems] = useState>(new Set()); const [downloadingItem, setDownloadingItem] = useState(null); - const [visibleCount, setVisibleCount] = useState(10); + const [visibleCount, setVisibleCount] = useState(MEDIA_LIST_PAGE_SIZE); const loadMoreRef = useRef(null); useEffect(() => { + const scrollElement = getContentScrollElement(); + if (!scrollElement) { + return; + } const handleScroll = () => { - setShowScrollTop(window.scrollY > 300); + setShowScrollTop(scrollElement.scrollTop > 300); }; - window.addEventListener("scroll", handleScroll); - return () => window.removeEventListener("scroll", handleScroll); + handleScroll(); + scrollElement.addEventListener("scroll", handleScroll); + return () => scrollElement.removeEventListener("scroll", handleScroll); }, []); const filteredTimeline = useMemo(() => { let filtered = [...timeline]; @@ -221,55 +249,50 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = return filtered; }, [timeline, filterType, sortBy]); useEffect(() => { - setVisibleCount(10); + setVisibleCount(MEDIA_LIST_PAGE_SIZE); }, [filteredTimeline.length]); useEffect(() => { const checkFoldersAndFFmpeg = async () => { const settings = getSettings(); const basePath = settings.downloadPath; const installed = await IsFFmpegInstalled(); + setCachedDependencyStatus("ffmpeg", { installed }); setFfmpegInstalled(installed); if (!basePath || !accountInfo.name) return; - let folderName = accountInfo.name; - if (accountInfo.nick === "My Bookmarks" || accountInfo.name === "bookmarks") { - folderName = "My Bookmarks"; - } - else if (accountInfo.nick === "My Likes" || accountInfo.name === "likes") { - folderName = "My Likes"; - } const exists = await CheckFolderExists(basePath, folderName); + const hasMP4 = exists ? await CheckGifsFolderHasMP4(basePath, folderName) : false; + setCachedMediaFolderStatus(basePath, folderName, { + folderExists: exists, + gifsFolderHasMP4: hasMP4, + }); setFolderExists(exists); - if (exists) { - const hasMP4 = await CheckGifsFolderHasMP4(basePath, folderName); - setGifsFolderHasMP4(hasMP4); - } - else { - setGifsFolderHasMP4(false); - } + setGifsFolderHasMP4(hasMP4); }; checkFoldersAndFFmpeg(); - }, [accountInfo.name, accountInfo.nick, hasDownloaded]); + }, [accountInfo.name, accountInfo.nick, folderName, hasDownloaded]); useEffect(() => { - if (!loadMoreRef.current) + const sentinel = loadMoreRef.current; + if (!sentinel || visibleCount >= filteredTimeline.length) return; const observer = new IntersectionObserver((entries) => { - if (entries[0].isIntersecting) { + if (entries.some((entry) => entry.isIntersecting)) { setVisibleCount((prev) => { const filteredLength = filteredTimeline.length; - return Math.min(prev + 10, filteredLength); + return Math.min(prev + MEDIA_LIST_PAGE_SIZE, filteredLength); }); } - }, { threshold: 0.1 }); - observer.observe(loadMoreRef.current); + }, { + rootMargin: "400px 0px", + threshold: 0, + }); + observer.observe(sentinel); return () => { - if (loadMoreRef.current) { - observer.unobserve(loadMoreRef.current); - } + observer.disconnect(); }; - }, [filteredTimeline.length]); + }, [visibleCount, filteredTimeline.length]); const scrollToTop = () => { - window.scrollTo({ top: 0, behavior: "smooth" }); + getContentScrollElement()?.scrollTo({ top: 0, behavior: "smooth" }); }; const openPreview = (index: number) => { setPreviewIndex(index); @@ -442,7 +465,14 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); - if (response.success) { + if (response.cancelled) { + logger.info(response.message || "Download stopped"); + toast.info(response.message || "Download stopped"); + if (response.downloaded > 0 || response.skipped > 0) { + setHasDownloaded(true); + } + } + else if (response.success) { const parts: string[] = []; if (response.downloaded > 0) { parts.push(`${response.downloaded} file${response.downloaded !== 1 ? 's' : ''} downloaded`); @@ -464,7 +494,17 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = } setHasDownloaded(true); const installed = await IsFFmpegInstalled(); + setCachedDependencyStatus("ffmpeg", { installed }); setFfmpegInstalled(installed); + if (settings.downloadPath) { + const hasMP4 = await CheckGifsFolderHasMP4(settings.downloadPath, folderName); + setCachedMediaFolderStatus(settings.downloadPath, folderName, { + folderExists: true, + gifsFolderHasMP4: hasMP4, + }); + setFolderExists(true); + setGifsFolderHasMP4(hasMP4); + } } else { logger.error(response.message); @@ -551,15 +591,11 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = if (response.success) { logger.success(`Converted ${response.converted} GIFs`); toast.success(`${response.converted} GIFs converted`); - const settings = getSettings(); - let folderName = accountInfo.name; - if (accountInfo.nick === "My Bookmarks" || accountInfo.name === "bookmarks") { - folderName = "My Bookmarks"; - } - else if (accountInfo.nick === "My Likes" || accountInfo.name === "likes") { - folderName = "My Likes"; - } const hasMP4 = await CheckGifsFolderHasMP4(settings.downloadPath, folderName); + setCachedMediaFolderStatus(settings.downloadPath, folderName, { + folderExists: true, + gifsFolderHasMP4: hasMP4, + }); setGifsFolderHasMP4(hasMP4); } else { @@ -800,7 +836,10 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); - if (response.success) { + if (response.cancelled) { + toast.info(response.message || "Download stopped"); + } + else if (response.success) { setHasDownloaded(true); } else { @@ -870,7 +909,10 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); - if (response.success) { + if (response.cancelled) { + toast.info(response.message || "Download stopped"); + } + else if (response.success) { setHasDownloaded(true); } else { @@ -946,8 +988,13 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType =
)} - {visibleCount < filteredTimeline.length && (
+ {visibleCount < filteredTimeline.length && (
+
)} @@ -1035,7 +1082,10 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = proxy: settings.proxy || "", }); const response = await DownloadMediaWithMetadata(request); - if (response.success) { + if (response.cancelled) { + toast.info(response.message || "Download stopped"); + } + else if (response.success) { setHasDownloaded(true); } else { diff --git a/frontend/src/components/SearchBar.tsx b/frontend/src/components/SearchBar.tsx index 6726ee0..4a040cf 100644 --- a/frontend/src/components/SearchBar.tsx +++ b/frontend/src/components/SearchBar.tsx @@ -2,7 +2,7 @@ import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { InputWithContext } from "@/components/ui/input-with-context"; import { Label } from "@/components/ui/label"; -import { XCircle, Calendar, StopCircle, Globe, Lock, Bookmark, Heart, Key, Eye, EyeOff, ChevronDown, ChevronUp, Info, RotateCcw, Trash2, Clock, CloudDownload, User, Users, FileText, Hourglass, CheckCircle, AlertCircle, } from "lucide-react"; +import { XCircle, Calendar, StopCircle, Globe, Lock, Bookmark, Heart, Cookie, Eye, EyeOff, ChevronDown, ChevronUp, Info, RotateCcw, Trash2, Clock, CloudDownload, User, Users, FileText, Hourglass, CheckCircle, AlertCircle, SlidersHorizontal, } from "lucide-react"; import { Spinner } from "@/components/ui/spinner"; function formatNumberWithComma(num: number): string { return num.toLocaleString(); @@ -10,16 +10,18 @@ function formatNumberWithComma(num: number): string { import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; -import { Settings as SettingsIcon } from "lucide-react"; import { FetchHistory } from "@/components/FetchHistory"; import type { HistoryItem } from "@/components/FetchHistory"; import { cn } from "@/lib/utils"; import { getSettings, updateSettings, type FetchMode as SettingsFetchMode, type MediaType as SettingsMediaType } from "@/lib/settings"; +import { GetStoredAuthToken, SetStoredAuthToken } from "../../wailsjs/go/main/App"; export type FetchMode = "public" | "private"; export type PrivateType = "bookmarks" | "likes"; export type FetchType = "single" | "multiple"; -const PUBLIC_AUTH_TOKEN_KEY = "twitter_public_auth_token"; -const PRIVATE_AUTH_TOKEN_KEY = "twitter_private_auth_token"; +const LEGACY_PUBLIC_AUTH_TOKEN_KEY = "twitter_public_auth_token"; +const LEGACY_PRIVATE_AUTH_TOKEN_KEY = "twitter_private_auth_token"; +let sessionPublicAuthToken = ""; +let sessionPrivateAuthToken = ""; export interface MultipleAccount { id: string; username: string; @@ -59,10 +61,10 @@ interface SearchBarProps { onFetchTypeChange?: (type: FetchType) => void; multipleAccounts?: MultipleAccount[]; onImportFile?: () => void; - onFetchAll?: () => void; + onFetchAll?: (authToken: string) => void; onStopAll?: () => void; onStopAccount?: (accountId: string) => void; - onRetryAccount?: (accountId: string) => void; + onRetryAccount?: (accountId: string, authToken: string) => void; isFetchingAll?: boolean; mode?: FetchMode; privateType?: PrivateType; @@ -74,38 +76,77 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop const [endDate, setEndDate] = useState(""); const [mediaType, setMediaType] = useState(getSettings().mediaType); const [retweets, setRetweets] = useState(getSettings().includeRetweets); - const [mode, setMode] = useState(externalMode || "public"); - const [privateType, setPrivateType] = useState(externalPrivateType || "bookmarks"); - useEffect(() => { - if (externalMode !== undefined) { - setMode(externalMode); - } - }, [externalMode]); - useEffect(() => { - if (externalPrivateType !== undefined) { - setPrivateType(externalPrivateType); - } - }, [externalPrivateType]); + const mode = externalMode || "public"; + const privateType = externalPrivateType || "bookmarks"; const [showAuthInput, setShowAuthInput] = useState(false); const [showAdvanced, setShowAdvanced] = useState(false); const [currentFetchMode, setCurrentFetchMode] = useState(getSettings().fetchMode); - const [publicAuthToken, setPublicAuthToken] = useState(""); - const [privateAuthToken, setPrivateAuthToken] = useState(""); + const [publicAuthToken, setPublicAuthToken] = useState(() => sessionPublicAuthToken || localStorage.getItem(LEGACY_PUBLIC_AUTH_TOKEN_KEY) || ""); + const [privateAuthToken, setPrivateAuthToken] = useState(() => sessionPrivateAuthToken || localStorage.getItem(LEGACY_PRIVATE_AUTH_TOKEN_KEY) || ""); const [showPublicToken, setShowPublicToken] = useState(false); const [showPrivateToken, setShowPrivateToken] = useState(false); useEffect(() => { - const savedPublicToken = localStorage.getItem(PUBLIC_AUTH_TOKEN_KEY) || ""; - const savedPrivateToken = localStorage.getItem(PRIVATE_AUTH_TOKEN_KEY) || ""; - setPublicAuthToken(savedPublicToken); - setPrivateAuthToken(savedPrivateToken); + let active = true; + const syncStoredTokens = async () => { + const legacyPublicToken = localStorage.getItem(LEGACY_PUBLIC_AUTH_TOKEN_KEY) || ""; + const legacyPrivateToken = localStorage.getItem(LEGACY_PRIVATE_AUTH_TOKEN_KEY) || ""; + try { + if (legacyPublicToken) { + await SetStoredAuthToken("public", legacyPublicToken); + } + if (legacyPrivateToken) { + await SetStoredAuthToken("private", legacyPrivateToken); + } + } + catch (error) { + console.error("Failed to migrate legacy auth token:", error); + } + finally { + localStorage.removeItem(LEGACY_PUBLIC_AUTH_TOKEN_KEY); + localStorage.removeItem(LEGACY_PRIVATE_AUTH_TOKEN_KEY); + } + + try { + const [storedPublicToken, storedPrivateToken] = await Promise.all([ + GetStoredAuthToken("public"), + GetStoredAuthToken("private"), + ]); + if (!active) { + return; + } + sessionPublicAuthToken = storedPublicToken || ""; + setPublicAuthToken(storedPublicToken || ""); + sessionPrivateAuthToken = storedPrivateToken || ""; + setPrivateAuthToken(storedPrivateToken || ""); + } + catch (error) { + console.error("Failed to load auth token from secure storage:", error); + } + }; + void syncStoredTokens(); + return () => { + active = false; + }; }, []); - const handlePublicTokenChange = (value: string) => { + const handlePublicTokenChange = async (value: string) => { + sessionPublicAuthToken = value; setPublicAuthToken(value); - localStorage.setItem(PUBLIC_AUTH_TOKEN_KEY, value); + try { + await SetStoredAuthToken("public", value); + } + catch (error) { + console.error("Failed to store public auth token securely:", error); + } }; - const handlePrivateTokenChange = (value: string) => { + const handlePrivateTokenChange = async (value: string) => { + sessionPrivateAuthToken = value; setPrivateAuthToken(value); - localStorage.setItem(PRIVATE_AUTH_TOKEN_KEY, value); + try { + await SetStoredAuthToken("private", value); + } + catch (error) { + console.error("Failed to store private auth token securely:", error); + } }; const handleFetch = () => { const authToken = mode === "public" ? publicAuthToken : privateAuthToken; @@ -145,7 +186,6 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop
{fetchType === "single" && (
- @@ -287,7 +326,6 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop {mode === "private" && fetchType === "single" && (<>
@@ -439,9 +476,9 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop {useDateRange && (<> - setStartDate(e.target.value)} className="w-[140px] h-8 bg-background"/> + setStartDate(e.target.value)} className="w-35 h-8 bg-background"/> - - setEndDate(e.target.value)} className="w-[140px] h-8 bg-background"/> + setEndDate(e.target.value)} className="w-35 h-8 bg-background"/> )} )}
@@ -483,7 +520,7 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop {fetchType === "multiple" && multipleAccounts.length > 0 && (

Accounts to Fetch ({formatNumberWithComma(multipleAccounts.length)})

-
+
{multipleAccounts.map((account) => (
{account.accountInfo ? ({account.accountInfo.nick}) : (
@@ -509,7 +546,7 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop
{account.status === "fetching" && (<> -
+
{account.remainingTime !== null && account.remainingTime >= 0 @@ -535,7 +572,7 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop Incomplete - @@ -545,7 +582,7 @@ export function SearchBar({ username, loading, onUsernameChange, onFetch, onStop Failed - diff --git a/frontend/src/components/SettingsPage.tsx b/frontend/src/components/SettingsPage.tsx index 132b3c1..52cbb9b 100644 --- a/frontend/src/components/SettingsPage.tsx +++ b/frontend/src/components/SettingsPage.tsx @@ -1,28 +1,180 @@ -import { useState, useEffect } from "react"; +import { useEffect, useState, useCallback } from "react"; +import { flushSync } from "react-dom"; import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; import { InputWithContext } from "@/components/ui/input-with-context"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; -import { FolderOpen, Save, RotateCcw, Info, Download, Check } from "lucide-react"; +import { Check, Download, ExternalLink, FolderOpen, Info, MonitorCog, PackageSearch, Plus, RotateCcw, Save, Trash2 } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Spinner } from "@/components/ui/spinner"; import { Switch } from "@/components/ui/switch"; -import { getSettings, getSettingsWithDefaults, saveSettings, resetToDefaultSettings, applyThemeMode, applyFont, FONT_OPTIONS, type Settings as SettingsType, type FontFamily, type GifQuality, type GifResolution } from "@/lib/settings"; +import { getSettings, getSettingsWithDefaults, saveSettings, resetToDefaultSettings, applyThemeMode, applyFont, getFontOptions, parseGoogleFontUrl, loadGoogleFontUrl, loadCustomFonts, saveCustomFonts, type Settings as SettingsType, type FontFamily, type GifQuality, type GifResolution, type CustomFontFamily } from "@/lib/settings"; +import { getCachedDependencyStatus, setCachedDependencyStatus } from "@/lib/runtime-cache"; import { themes, applyTheme } from "@/lib/themes"; +import { compareVersionNumbers } from "@/lib/version"; import { SelectFolder, IsExtractorInstalled, DownloadExtractor, IsFFmpegInstalled, DownloadFFmpeg, IsExifToolInstalled, DownloadExifTool } from "../../wailsjs/go/main/App"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; -export function SettingsPage() { + +interface DependencyVersionStatus { + installed: boolean; + installed_version?: string; + latest_version?: string; +} + +type DependencyVersionMethod = "GetExtractorVersionStatus" | "GetFFmpegVersionStatus" | "GetExifToolVersionStatus"; +type SettingsTab = "general" | "dependencies"; + +interface SettingsPageProps { + onUnsavedChangesChange?: (hasUnsavedChanges: boolean) => void; + onResetRequest?: (resetFn: () => void) => void; +} + +function getDependencyVersionStatus(methodName: DependencyVersionMethod) { + const app = (window as Window & { + go?: { + main?: { + App?: Partial Promise>>; + }; + }; + }).go?.main?.App; + const method = app?.[methodName]; + return method ? method() : Promise.resolve(null); +} + +function hasNewDependencyVersion(installedVersion: string | null, latestVersion: string | null) { + if (!installedVersion || !latestVersion) { + return false; + } + return compareVersionNumbers(latestVersion, installedVersion) > 0; +} + +function buildDependencyVersionText(name: string, installed: boolean, installedVersion: string | null, latestVersion: string | null, updateAvailable: boolean) { + if (installed) { + if (!installedVersion) { + return `${name} installed`; + } + return updateAvailable && latestVersion + ? `${name} ${installedVersion} (New Version Available: ${latestVersion})` + : `${name} ${installedVersion}`; + } + return latestVersion ? `Latest ${name}: ${latestVersion}` : null; +} + +export function SettingsPage({ onUnsavedChangesChange, onResetRequest }: SettingsPageProps) { + const cachedExtractorStatus = getCachedDependencyStatus("extractor"); + const cachedFfmpegStatus = getCachedDependencyStatus("ffmpeg"); + const cachedExiftoolStatus = getCachedDependencyStatus("exiftool"); const [savedSettings, setSavedSettings] = useState(getSettings()); const [tempSettings, setTempSettings] = useState(savedSettings); - const [isDark, setIsDark] = useState(document.documentElement.classList.contains('dark')); - const [extractorInstalled, setExtractorInstalled] = useState(false); + const [isDark, setIsDark] = useState(document.documentElement.classList.contains("dark")); + const [activeTab, setActiveTab] = useState("general"); + const [showAddFontDialog, setShowAddFontDialog] = useState(false); + const [addFontUrl, setAddFontUrl] = useState(""); + const [extractorInstalled, setExtractorInstalled] = useState(cachedExtractorStatus.installed ?? false); + const [extractorInstalledVersion, setExtractorInstalledVersion] = useState(cachedExtractorStatus.installedVersion); + const [extractorLatestVersion, setExtractorLatestVersion] = useState(cachedExtractorStatus.latestVersion); const [downloadingExtractor, setDownloadingExtractor] = useState(false); - const [ffmpegInstalled, setFfmpegInstalled] = useState(false); + const [ffmpegInstalled, setFfmpegInstalled] = useState(cachedFfmpegStatus.installed ?? false); + const [ffmpegInstalledVersion, setFfmpegInstalledVersion] = useState(cachedFfmpegStatus.installedVersion); + const [ffmpegLatestVersion, setFfmpegLatestVersion] = useState(cachedFfmpegStatus.latestVersion); const [downloadingFFmpeg, setDownloadingFFmpeg] = useState(false); - const [exiftoolInstalled, setExiftoolInstalled] = useState(false); + const [exiftoolInstalled, setExiftoolInstalled] = useState(cachedExiftoolStatus.installed ?? false); + const [exiftoolInstalledVersion, setExiftoolInstalledVersion] = useState(cachedExiftoolStatus.installedVersion); + const [exiftoolLatestVersion, setExiftoolLatestVersion] = useState(cachedExiftoolStatus.latestVersion); const [downloadingExifTool, setDownloadingExifTool] = useState(false); const [showResetConfirm, setShowResetConfirm] = useState(false); + + const extractorUpdateAvailable = hasNewDependencyVersion(extractorInstalledVersion, extractorLatestVersion); + const ffmpegUpdateAvailable = hasNewDependencyVersion(ffmpegInstalledVersion, ffmpegLatestVersion); + const exiftoolUpdateAvailable = hasNewDependencyVersion(exiftoolInstalledVersion, exiftoolLatestVersion); + const parsedAddFont = parseGoogleFontUrl(addFontUrl); + const fontOptions = getFontOptions(tempSettings.customFonts); + const hasUnsavedChanges = JSON.stringify(savedSettings) !== JSON.stringify(tempSettings); + + const resetToSaved = useCallback(() => { + const freshSavedSettings = getSettings(); + flushSync(() => { + setTempSettings(freshSavedSettings); + setIsDark(document.documentElement.classList.contains("dark")); + }); + }, []); + + useEffect(() => { + if (onResetRequest) { + onResetRequest(resetToSaved); + } + }, [onResetRequest, resetToSaved]); + + useEffect(() => { + onUnsavedChangesChange?.(hasUnsavedChanges); + }, [hasUnsavedChanges, onUnsavedChangesChange]); + + const extractorVersionText = extractorInstalled + ? extractorInstalledVersion + ? extractorUpdateAvailable && extractorLatestVersion + ? `${extractorInstalledVersion} (New Version Available: ${extractorLatestVersion})` + : extractorInstalledVersion + : "Installed (version unavailable)" + : extractorLatestVersion + ? `Latest: ${extractorLatestVersion}` + : null; + + const ffmpegVersionText = buildDependencyVersionText("FFmpeg", ffmpegInstalled, ffmpegInstalledVersion, ffmpegLatestVersion, ffmpegUpdateAvailable); + const exiftoolVersionText = buildDependencyVersionText("ExifTool", exiftoolInstalled, exiftoolInstalledVersion, exiftoolLatestVersion, exiftoolUpdateAvailable); + + const loadDependencyStatus = async () => { + try { + const [extractorVersionStatus, ffmpegVersionStatus, exiftoolVersionStatus, extractor, ffmpeg, exiftool] = await Promise.all([ + getDependencyVersionStatus("GetExtractorVersionStatus"), + getDependencyVersionStatus("GetFFmpegVersionStatus"), + getDependencyVersionStatus("GetExifToolVersionStatus"), + IsExtractorInstalled(), + IsFFmpegInstalled(), + IsExifToolInstalled(), + ]); + const nextExtractorInstalled = extractorVersionStatus?.installed ?? extractor; + const nextExtractorInstalledVersion = extractorVersionStatus?.installed_version?.trim() || null; + const nextExtractorLatestVersion = extractorVersionStatus?.latest_version?.trim() || null; + const nextFfmpegInstalled = ffmpegVersionStatus?.installed ?? ffmpeg; + const nextFfmpegInstalledVersion = ffmpegVersionStatus?.installed_version?.trim() || null; + const nextFfmpegLatestVersion = ffmpegVersionStatus?.latest_version?.trim() || null; + const nextExiftoolInstalled = exiftoolVersionStatus?.installed ?? exiftool; + const nextExiftoolInstalledVersion = exiftoolVersionStatus?.installed_version?.trim() || null; + const nextExiftoolLatestVersion = exiftoolVersionStatus?.latest_version?.trim() || null; + + setCachedDependencyStatus("extractor", { + installed: nextExtractorInstalled, + installedVersion: nextExtractorInstalledVersion, + latestVersion: nextExtractorLatestVersion, + }); + setCachedDependencyStatus("ffmpeg", { + installed: nextFfmpegInstalled, + installedVersion: nextFfmpegInstalledVersion, + latestVersion: nextFfmpegLatestVersion, + }); + setCachedDependencyStatus("exiftool", { + installed: nextExiftoolInstalled, + installedVersion: nextExiftoolInstalledVersion, + latestVersion: nextExiftoolLatestVersion, + }); + + setExtractorInstalled(nextExtractorInstalled); + setExtractorInstalledVersion(nextExtractorInstalledVersion); + setExtractorLatestVersion(nextExtractorLatestVersion); + setFfmpegInstalled(nextFfmpegInstalled); + setFfmpegInstalledVersion(nextFfmpegInstalledVersion); + setFfmpegLatestVersion(nextFfmpegLatestVersion); + setExiftoolInstalled(nextExiftoolInstalled); + setExiftoolInstalledVersion(nextExiftoolInstalledVersion); + setExiftoolLatestVersion(nextExiftoolLatestVersion); + } + catch (error) { + console.error("Failed to check dependency status:", error); + } + }; + useEffect(() => { applyThemeMode(savedSettings.themeMode); applyTheme(savedSettings.theme); @@ -36,30 +188,23 @@ export function SettingsPage() { mediaQuery.addEventListener("change", handleChange); return () => mediaQuery.removeEventListener("change", handleChange); }, [savedSettings.themeMode, savedSettings.theme]); + useEffect(() => { applyThemeMode(tempSettings.themeMode); applyTheme(tempSettings.theme); - applyFont(tempSettings.fontFamily); + applyFont(tempSettings.fontFamily, tempSettings.customFonts); setTimeout(() => { - setIsDark(document.documentElement.classList.contains('dark')); + setIsDark(document.documentElement.classList.contains("dark")); }, 0); - }, [tempSettings.themeMode, tempSettings.theme, tempSettings.fontFamily]); + }, [tempSettings.themeMode, tempSettings.theme, tempSettings.fontFamily, tempSettings.customFonts]); + + useEffect(() => { + if (showAddFontDialog && parsedAddFont) { + loadGoogleFontUrl(parsedAddFont.url, "twitter-media-add-font-preview"); + } + }, [showAddFontDialog, parsedAddFont]); + useEffect(() => { - const loadDependencyStatus = async () => { - try { - const [extractor, ffmpeg, exiftool] = await Promise.all([ - IsExtractorInstalled(), - IsFFmpegInstalled(), - IsExifToolInstalled(), - ]); - setExtractorInstalled(extractor); - setFfmpegInstalled(ffmpeg); - setExiftoolInstalled(exiftool); - } - catch (error) { - console.error("Failed to check dependency status:", error); - } - }; const loadDefaults = async () => { if (!savedSettings.downloadPath) { const settingsWithDefaults = await getSettingsWithDefaults(); @@ -67,24 +212,37 @@ export function SettingsPage() { setTempSettings(settingsWithDefaults); } }; - loadDefaults(); - loadDependencyStatus(); + void loadDefaults(); + void loadDependencyStatus(); }, []); + + useEffect(() => { + const syncCustomFonts = async () => { + const customFonts = await loadCustomFonts(); + setSavedSettings((prev) => ({ ...prev, customFonts })); + setTempSettings((prev) => ({ ...prev, customFonts })); + }; + void syncCustomFonts(); + }, []); + const handleSave = () => { saveSettings(tempSettings); setSavedSettings(tempSettings); toast.success("Settings saved"); + onUnsavedChangesChange?.(false); }; + const handleReset = async () => { const defaultSettings = await resetToDefaultSettings(); setTempSettings(defaultSettings); setSavedSettings(defaultSettings); applyThemeMode(defaultSettings.themeMode); applyTheme(defaultSettings.theme); - applyFont(defaultSettings.fontFamily); + applyFont(defaultSettings.fontFamily, defaultSettings.customFonts); setShowResetConfirm(false); toast.success("Settings reset to default"); }; + const handleBrowseFolder = async () => { try { const selectedPath = await SelectFolder(tempSettings.downloadPath || ""); @@ -97,12 +255,66 @@ export function SettingsPage() { toast.error(`Error selecting folder: ${error}`); } }; + + const closeAddFontDialog = () => { + setShowAddFontDialog(false); + setAddFontUrl(""); + }; + + const handleAddFont = async () => { + if (!parsedAddFont) { + toast.error("Enter a valid Google Fonts URL"); + return; + } + const existingFonts = tempSettings.customFonts || []; + const existingIndex = existingFonts.findIndex((font) => font.value === parsedAddFont.value || font.url === parsedAddFont.url); + const customFonts = existingIndex >= 0 + ? existingFonts.map((font, index) => index === existingIndex ? parsedAddFont : font) + : [...existingFonts, parsedAddFont]; + const savedCustomFonts = await saveCustomFonts(customFonts); + setSavedSettings((prev) => ({ ...prev, customFonts: savedCustomFonts })); + setTempSettings((prev) => ({ + ...prev, + customFonts: savedCustomFonts, + fontFamily: parsedAddFont.value, + })); + closeAddFontDialog(); + toast.success(`${parsedAddFont.label} added`); + }; + + const handleDeleteCustomFont = async (fontValue: CustomFontFamily) => { + const customFonts = (tempSettings.customFonts || []).filter((font) => font.value !== fontValue); + const savedCustomFonts = await saveCustomFonts(customFonts); + const shouldResetSavedFont = savedSettings.fontFamily === fontValue; + const shouldResetTempFont = tempSettings.fontFamily === fontValue; + const nextSavedSettings: SettingsType = { + ...savedSettings, + customFonts: savedCustomFonts, + fontFamily: shouldResetSavedFont ? "google-sans" : savedSettings.fontFamily, + }; + setSavedSettings(nextSavedSettings); + setTempSettings((prev) => ({ + ...prev, + customFonts: savedCustomFonts, + fontFamily: shouldResetTempFont ? "google-sans" : prev.fontFamily, + })); + if (shouldResetSavedFont) { + saveSettings(nextSavedSettings); + } + toast.success("Font deleted"); + }; + const handleDownloadExtractor = async () => { setDownloadingExtractor(true); try { + const successMessage = extractorInstalled + ? extractorUpdateAvailable + ? "Xtractor updated successfully" + : "Xtractor reinstalled successfully" + : "Xtractor downloaded successfully"; await DownloadExtractor(); - setExtractorInstalled(true); - toast.success(extractorInstalled ? "Xtractor reinstalled successfully" : "Xtractor downloaded successfully"); + await loadDependencyStatus(); + toast.success(successMessage); } catch (error) { toast.error("Failed to download xtractor"); @@ -112,12 +324,18 @@ export function SettingsPage() { setDownloadingExtractor(false); } }; + const handleDownloadFFmpeg = async () => { setDownloadingFFmpeg(true); try { + const successMessage = ffmpegInstalled + ? ffmpegUpdateAvailable + ? "FFmpeg updated successfully" + : "FFmpeg reinstalled successfully" + : "FFmpeg downloaded successfully"; await DownloadFFmpeg(); - setFfmpegInstalled(true); - toast.success("FFmpeg downloaded successfully"); + await loadDependencyStatus(); + toast.success(successMessage); } catch (error) { toast.error("Failed to download FFmpeg"); @@ -127,12 +345,18 @@ export function SettingsPage() { setDownloadingFFmpeg(false); } }; + const handleDownloadExifTool = async () => { setDownloadingExifTool(true); try { + const successMessage = exiftoolInstalled + ? exiftoolUpdateAvailable + ? "ExifTool updated successfully" + : "ExifTool reinstalled successfully" + : "ExifTool downloaded successfully"; await DownloadExifTool(); - setExiftoolInstalled(true); - toast.success("ExifTool downloaded successfully"); + await loadDependencyStatus(); + toast.success(successMessage); } catch (error) { toast.error("Failed to download ExifTool"); @@ -142,269 +366,389 @@ export function SettingsPage() { setDownloadingExifTool(false); } }; - return (
-

Settings

-
- -
- -
- -
- setTempSettings((prev) => ({ ...prev, downloadPath: e.target.value }))} placeholder="C:\Users\YourUsername\Pictures"/> - -
-
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - setTempSettings(prev => ({ ...prev, sfxEnabled: checked }))}/> -
+ return (
+
+

Settings

+
+ +
+
- -
- -
- -
- {extractorInstalled && (
- - Installed -
)} - -
-
+
+ + +
- -
- -
- {exiftoolInstalled ? (
- - Installed -
) : ()} -
-
+
+ {activeTab === "general" && (
+
+
+ +
+ setTempSettings((prev) => ({ ...prev, downloadPath: e.target.value }))} placeholder="C:\Users\YourUsername\Pictures"/> + +
+
- -
- -
- {ffmpegInstalled ? (
- - Installed -
) : ()} -
-
- - - {ffmpegInstalled && (
- -
- setTempSettings((prev) => ({ ...prev, themeMode: value }))}> + + - Fast - Better - - -
-
)} - -
- - setTempSettings((prev) => ({ ...prev, proxy: e.target.value }))} placeholder="http://proxy:port or socks5://proxy:port (optional)" className="w-[90%]"/> -
+
+ + +
- -
- - { - const inputValue = e.target.value; - if (inputValue === "") { - setTempSettings((prev) => ({ ...prev, fetchTimeout: 60 })); - return; - } - const value = parseInt(inputValue, 10); - if (!isNaN(value)) { - setTempSettings((prev) => ({ ...prev, fetchTimeout: value })); - } - }} onBlur={(e) => { - const value = parseInt(e.target.value, 10); - if (isNaN(value) || value < 30) { - setTempSettings((prev) => ({ ...prev, fetchTimeout: 30 })); - } - else if (value > 900) { - setTempSettings((prev) => ({ ...prev, fetchTimeout: 900 })); - } - }} placeholder="60" className="w-[20%]"/> -
+
+ +
+ + +
+ {tempSettings.customFonts.length > 0 && (
+

Custom Fonts

+
+ {tempSettings.customFonts.map((font) => (
+
+

+ {font.label} +

+

{font.url}

+
+ +
))} +
+
)} +
-
+
+ + setTempSettings((prev) => ({ ...prev, sfxEnabled: checked }))}/> +
+
+ +
+
+ +
+
+ + +
+ +
+ + +
+ +
+ +
+ +
+ + setTempSettings((prev) => ({ ...prev, proxy: e.target.value }))} placeholder="http://proxy1:port, socks5://proxy2:port (optional)" className="max-w-md"/> +
+ +
+ + { + const inputValue = e.target.value; + if (inputValue === "") { + setTempSettings((prev) => ({ ...prev, fetchTimeout: 60 })); + return; + } + const value = parseInt(inputValue, 10); + if (!isNaN(value)) { + setTempSettings((prev) => ({ ...prev, fetchTimeout: value })); + } + }} onBlur={(e) => { + const value = parseInt(e.target.value, 10); + if (isNaN(value) || value < 30) { + setTempSettings((prev) => ({ ...prev, fetchTimeout: 30 })); + } + else if (value > 900) { + setTempSettings((prev) => ({ ...prev, fetchTimeout: 900 })); + } + }} placeholder="60" className="w-24"/> +
+
+
+
+
)} + + {activeTab === "dependencies" && (
+
+
+ +
+ + {extractorInstalled && (
+ + Installed +
)} +
+
+ +
+ +
+ + {ffmpegInstalled && (
+ + Installed +
)} +
+
+ +
+ +
+ + {exiftoolInstalled && (
+ + Installed +
)} +
+
+
+ +
+
)}
- -
- - -
+ open ? setShowAddFontDialog(true) : closeAddFontDialog()}> + + + + + +
+
+ + setAddFontUrl(event.target.value)} onKeyDown={(event) => { + if (event.key === "Enter" && parsedAddFont) { + void handleAddFont(); + } + }} placeholder="https://fonts.google.com/specimen/Ubuntu" autoFocus/> + {addFontUrl.trim() && !parsedAddFont && (

+ Enter a valid Google Fonts URL. +

)} +
+
+

Preview

+

+ Aa The quick brown fox +

+

+ Twitter/X Media Batch Downloader +

+
+
+ + + + +
+
- diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index 6c944a7..4c54311 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -1,18 +1,33 @@ +import { useState } from "react"; import { HomeIcon } from "@/components/ui/home"; import { SettingsIcon } from "@/components/ui/settings"; import { ArchiveIcon } from "@/components/ui/archive"; import { TerminalIcon } from "@/components/ui/terminal"; -import { GithubIcon } from "@/components/ui/github"; +import { BugReportIcon } from "@/components/ui/bug-report-icon"; import { CoffeeIcon } from "@/components/ui/coffee"; +import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { Checkbox } from "@/components/ui/checkbox"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { Button } from "@/components/ui/button"; import { openExternal } from "@/lib/utils"; -export type PageType = "main" | "settings" | "debug" | "database"; +export type PageType = "main" | "settings" | "debug" | "database" | "support"; interface SidebarProps { currentPage: PageType; onPageChange: (page: PageType) => void; } export function Sidebar({ currentPage, onPageChange }: SidebarProps) { + const [isIssuesDialogOpen, setIsIssuesDialogOpen] = useState(false); + const [hasIssueAgreement, setHasIssueAgreement] = useState(false); + const handleIssuesDialogChange = (open: boolean) => { + setIsIssuesDialogOpen(open); + if (!open) { + setHasIssueAgreement(false); + } + }; + const handleOpenIssues = () => { + openExternal("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader/issues"); + handleIssuesDialogChange(false); + }; return (
@@ -55,7 +70,7 @@ export function Sidebar({ currentPage, onPageChange }: SidebarProps) { @@ -66,20 +81,53 @@ export function Sidebar({ currentPage, onPageChange }: SidebarProps) {
+ + + + + + +

Report Bugs or Request Features

+
+
+ + + Before Opening GitHub Issues + + + +
+
+

Important

+

+ Search existing issues first and use the issue template when opening a new bug report or feature request. +

+
+ + +
+ + + + + +
+
- - - -

Report Bug

-
-
- - - diff --git a/frontend/src/components/SupportPage.tsx b/frontend/src/components/SupportPage.tsx new file mode 100644 index 0000000..bcfc8db --- /dev/null +++ b/frontend/src/components/SupportPage.tsx @@ -0,0 +1,59 @@ +import { useState } from "react"; +import { Button } from "@/components/ui/button"; +import { CircleCheck, Copy } from "lucide-react"; +import { openExternal } from "@/lib/utils"; +import KofiLogo from "@/assets/ko-fi.gif"; +import KofiSvg from "@/assets/kofi_symbol.svg"; +import UsdtBarcode from "@/assets/usdt.jpg"; + +const USDT_ADDRESS = "THnzAAwZgp2Sq5CAXLP2njQDhTvgZG9EWs"; + +export function SupportPage() { + const [copiedUsdt, setCopiedUsdt] = useState(false); + + return (
+
+
+
+
+ Ko-fi +
+

Support via Ko-fi

+

+ Enjoying the project? You can support ongoing development by buying me a coffee. +

+
+ +
+ +
+
+
+
+ USDT Barcode +
+
+

USDT (TRC20)

+

+ Crypto donations are also accepted. Scan the QR code or copy the address. +

+
+
+ + {USDT_ADDRESS} + + +
+
+
+
); +} diff --git a/frontend/src/components/ui/bug-report-icon.tsx b/frontend/src/components/ui/bug-report-icon.tsx new file mode 100644 index 0000000..aea9608 --- /dev/null +++ b/frontend/src/components/ui/bug-report-icon.tsx @@ -0,0 +1,131 @@ +"use client"; + +import type { Transition, Variants } from "motion/react"; +import { AnimatePresence, motion } from "motion/react"; +import { useEffect, useState, type HTMLAttributes } from "react"; +import { cn } from "@/lib/utils"; + +type ReportIconMode = "bug" | "bulb"; + +interface BugReportIconProps extends HTMLAttributes { + size?: number; + loop?: boolean; +} + +const LOOP_INTERVAL_MS = 2200; + +const GROUP_VARIANTS: Variants = { + hidden: { + opacity: 0, + }, + visible: { + opacity: 1, + transition: { + duration: 0.2, + ease: [0, 0, 0.2, 1], + }, + }, + exit: { + opacity: 0, + transition: { + duration: 0.18, + ease: [0.4, 0, 1, 1], + }, + }, +}; + +const DRAW_VARIANTS: Variants = { + hidden: { + pathLength: 0, + opacity: 0, + }, + visible: { + pathLength: 1, + opacity: 1, + }, + exit: { + pathLength: 1, + opacity: 0, + }, +}; + +function createDrawTransition(delay = 0, duration = 0.36): Transition { + return { + duration, + delay, + ease: [0.4, 0, 0.2, 1], + opacity: { delay }, + }; +} + +function BugPaths() { + return (<> + + + + + + + + + + + + ); +} + +function BulbPaths() { + return (<> + + + + ); +} + +function ReportIconGroup({ mode }: { mode: ReportIconMode; }) { + return ( + {mode === "bug" ? : } + ); +} + +function StaticBugIcon() { + return ( + + + + + + + + + + + + ); +} + +function BugReportIcon({ className, size = 28, loop = false, ...props }: BugReportIconProps) { + const [mode, setMode] = useState("bug"); + + useEffect(() => { + if (!loop) { + return; + } + + const intervalId = window.setInterval(() => { + setMode((currentMode) => currentMode === "bug" ? "bulb" : "bug"); + }, LOOP_INTERVAL_MS); + + return () => window.clearInterval(intervalId); + }, [loop]); + + return (
+ + {loop ? ( + + ) : ()} + +
); +} + +export { BugReportIcon }; diff --git a/frontend/src/components/ui/coffee.tsx b/frontend/src/components/ui/coffee.tsx index a1432d5..e837464 100644 --- a/frontend/src/components/ui/coffee.tsx +++ b/frontend/src/components/ui/coffee.tsx @@ -10,6 +10,7 @@ export interface CoffeeIconHandle { } interface CoffeeIconProps extends HTMLAttributes { size?: number; + loop?: boolean; } const PATH_VARIANTS: Variants = { normal: { @@ -27,7 +28,7 @@ const PATH_VARIANTS: Variants = { }, }), }; -const CoffeeIcon = forwardRef(({ onMouseEnter, onMouseLeave, className, size = 28, ...props }, ref) => { +const CoffeeIcon = forwardRef(({ onMouseEnter, onMouseLeave, className, size = 28, loop = false, ...props }, ref) => { const controls = useAnimation(); const isControlledRef = useRef(false); useImperativeHandle(ref, () => { @@ -55,9 +56,9 @@ const CoffeeIcon = forwardRef(({ onMouseEnter }, [controls, onMouseLeave]); return (
- - - + + +
); diff --git a/frontend/src/components/ui/input-with-context.tsx b/frontend/src/components/ui/input-with-context.tsx index 1c3ee0e..1e25907 100644 --- a/frontend/src/components/ui/input-with-context.tsx +++ b/frontend/src/components/ui/input-with-context.tsx @@ -9,6 +9,13 @@ const InputWithContext = React.forwardRef(null); const [hasSelection, setHasSelection] = React.useState(false); const [canPaste, setCanPaste] = React.useState(false); + const [uncontrolledHasValue, setUncontrolledHasValue] = React.useState(() => { + const defaultValue = props.defaultValue; + return defaultValue !== undefined && defaultValue !== null && String(defaultValue).length > 0; + }); + const hasValue = props.value !== undefined && props.value !== null + ? String(props.value).length > 0 + : uncontrolledHasValue; React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement); const updateSelectionState = () => { const input = inputRef.current; @@ -50,6 +57,9 @@ const InputWithContext = React.forwardRef 0); + } input.focus(); } catch (err) { @@ -96,6 +106,9 @@ const InputWithContext = React.forwardRef 0); + } input.focus(); await checkClipboard(); } @@ -118,6 +131,9 @@ const InputWithContext = React.forwardRef 0); + } }; return ( { if (open) { @@ -144,7 +160,7 @@ const InputWithContext = React.forwardRefCtrl+V - + Select All Ctrl+A diff --git a/frontend/src/components/ui/terminal.tsx b/frontend/src/components/ui/terminal.tsx index f55b59a..6acbc2e 100644 --- a/frontend/src/components/ui/terminal.tsx +++ b/frontend/src/components/ui/terminal.tsx @@ -10,6 +10,7 @@ export interface TerminalIconHandle { } interface TerminalIconProps extends HTMLAttributes { size?: number; + loop?: boolean; } const LINE_VARIANTS: Variants = { normal: { opacity: 1 }, @@ -22,7 +23,7 @@ const LINE_VARIANTS: Variants = { }, }, }; -const TerminalIcon = forwardRef(({ onMouseEnter, onMouseLeave, className, size = 28, ...props }, ref) => { +const TerminalIcon = forwardRef(({ onMouseEnter, onMouseLeave, className, size = 28, loop = false, ...props }, ref) => { const controls = useAnimation(); const isControlledRef = useRef(false); useImperativeHandle(ref, () => { @@ -51,7 +52,7 @@ const TerminalIcon = forwardRef(({ onMous return (
- +
); }); diff --git a/frontend/src/index.css b/frontend/src/index.css index 6d4855e..399c151 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -72,6 +72,13 @@ } @layer base { + html, + body, + #root { + height: 100%; + overflow: hidden; + } + * { @apply border-border outline-ring/50; } @@ -234,6 +241,28 @@ scrollbar-color: var(--primary) var(--secondary); } +input::-ms-reveal, +input::-ms-clear { + display: none; +} + +input[type="number"] { + color-scheme: light; +} + +.dark input[type="number"] { + color-scheme: dark; +} + +input[type="number"]::-webkit-outer-spin-button, +input[type="number"]::-webkit-inner-spin-button { + margin: 0; + background: transparent; + border: 0; + box-shadow: none; + opacity: 1; +} + /* Custom scrollbar class untuk komponen tertentu */ .custom-scrollbar::-webkit-scrollbar { width: 6px; diff --git a/frontend/src/lib/audio.ts b/frontend/src/lib/audio.ts index 241d183..cdb8e5e 100644 --- a/frontend/src/lib/audio.ts +++ b/frontend/src/lib/audio.ts @@ -2,7 +2,15 @@ class AudioManager { private audioContext: AudioContext | null = null; private getAudioContext(): AudioContext { if (!this.audioContext) { - this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)(); + const AudioContextConstructor = window.AudioContext || (window as Window & { + webkitAudioContext?: typeof AudioContext; + }).webkitAudioContext; + if (!AudioContextConstructor) { + throw new Error("Web Audio API is not supported in this browser"); + } + const context = new AudioContextConstructor(); + this.audioContext = context; + return context; } return this.audioContext; } diff --git a/frontend/src/lib/fetch-state.ts b/frontend/src/lib/fetch-state.ts index c72035b..9dd32c0 100644 --- a/frontend/src/lib/fetch-state.ts +++ b/frontend/src/lib/fetch-state.ts @@ -1,19 +1,32 @@ -import type { TwitterResponse, TimelineEntry, AccountInfo } from "@/types/api"; +import type { TimelineEntry } from "@/types/api"; const FETCH_STATE_KEY = "twitter_fetch_state"; const CURSOR_STATE_KEY = "twitter_cursor_state"; export interface FetchState { username: string; cursor: string; - timeline: TimelineEntry[]; - accountInfo: AccountInfo | null; totalFetched: number; completed: boolean; lastUpdated: number; - authToken: string; mediaType: string; retweets: boolean; timelineType: string; } + +function normalizeFetchState(state: FetchState | null | undefined): FetchState | null { + if (!state) { + return null; + } + + return { + ...state, + cursor: state.cursor || "", + totalFetched: state.totalFetched ?? 0, + mediaType: state.mediaType || "all", + retweets: state.retweets ?? false, + timelineType: state.timelineType || "timeline", + }; +} + export function saveFetchState(state: Partial & { username: string; }): void { @@ -22,12 +35,9 @@ export function saveFetchState(state: Partial & { const updated: FetchState = { username: state.username, cursor: state.cursor || "", - timeline: state.timeline || existing?.timeline || [], - accountInfo: state.accountInfo || existing?.accountInfo || null, totalFetched: state.totalFetched ?? existing?.totalFetched ?? 0, completed: state.completed ?? false, lastUpdated: Date.now(), - authToken: state.authToken || existing?.authToken || "", mediaType: state.mediaType || existing?.mediaType || "all", retweets: state.retweets ?? existing?.retweets ?? false, timelineType: state.timelineType || existing?.timelineType || "timeline", @@ -43,7 +53,7 @@ export function saveFetchState(state: Partial & { export function getFetchState(username: string): FetchState | null { try { const allStates = getAllFetchStates(); - return allStates[username.toLowerCase()] || null; + return normalizeFetchState(allStates[username.toLowerCase()]); } catch (error) { console.error("Failed to get fetch state:", error); @@ -54,7 +64,17 @@ export function getAllFetchStates(): Record { try { const stored = localStorage.getItem(FETCH_STATE_KEY); if (stored) { - return JSON.parse(stored); + const parsed = JSON.parse(stored); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + const normalized: Record = {}; + for (const [key, value] of Object.entries(parsed as Record)) { + const state = normalizeFetchState(value); + if (state) { + normalized[key] = state; + } + } + return normalized; + } } } catch (error) { @@ -66,7 +86,7 @@ export function hasResumableFetch(username: string): boolean { const state = getFetchState(username); if (!state) return false; - return !!state.cursor && !state.completed && state.timeline.length > 0; + return !!state.cursor && !state.completed && state.totalFetched > 0; } export function getResumableInfo(username: string): { canResume: boolean; @@ -79,7 +99,7 @@ export function getResumableInfo(username: string): { } return { canResume: true, - mediaCount: state.timeline.length, + mediaCount: state.totalFetched, lastUpdated: new Date(state.lastUpdated), }; } @@ -108,25 +128,6 @@ export function clearAllIncompleteFetchStates(): void { console.error("Failed to clear fetch states:", error); } } -export function stateToResponse(state: FetchState): TwitterResponse | null { - if (!state.accountInfo) - return null; - return { - account_info: state.accountInfo, - total_urls: state.timeline.length, - timeline: state.timeline, - metadata: { - new_entries: state.timeline.length, - page: 0, - batch_size: 0, - has_more: !state.completed, - cursor: state.cursor, - completed: state.completed, - }, - cursor: state.cursor, - completed: state.completed, - }; -} export function saveCursor(username: string, cursor: string): void { try { const allCursors = getAllCursors(); @@ -176,7 +177,9 @@ export function clearCursor(username: string): void { } } export function mergeTimelines(existing: TimelineEntry[], newEntries: TimelineEntry[]): TimelineEntry[] { - const seenIds = new Set(existing.map((e) => e.tweet_id)); - const unique = newEntries.filter((e) => !seenIds.has(e.tweet_id)); - return [...existing, ...unique]; + const existingTimeline = Array.isArray(existing) ? existing : []; + const incomingTimeline = Array.isArray(newEntries) ? newEntries : []; + const seenIds = new Set(existingTimeline.map((e) => e.tweet_id)); + const unique = incomingTimeline.filter((e) => !seenIds.has(e.tweet_id)); + return [...existingTimeline, ...unique]; } diff --git a/frontend/src/lib/runtime-cache.ts b/frontend/src/lib/runtime-cache.ts new file mode 100644 index 0000000..9d21352 --- /dev/null +++ b/frontend/src/lib/runtime-cache.ts @@ -0,0 +1,55 @@ +export type DependencyName = "extractor" | "ffmpeg" | "exiftool"; + +export interface CachedDependencyStatus { + installed: boolean | null; + installedVersion: string | null; + latestVersion: string | null; +} + +export interface CachedMediaFolderStatus { + folderExists: boolean; + gifsFolderHasMP4: boolean; +} + +const dependencyStatusCache: Record = { + extractor: { + installed: null, + installedVersion: null, + latestVersion: null, + }, + ffmpeg: { + installed: null, + installedVersion: null, + latestVersion: null, + }, + exiftool: { + installed: null, + installedVersion: null, + latestVersion: null, + }, +}; + +const mediaFolderStatusCache = new Map(); + +function getMediaFolderStatusKey(downloadPath: string, folderName: string): string { + return `${downloadPath}::${folderName}`; +} + +export function getCachedDependencyStatus(name: DependencyName): CachedDependencyStatus { + return { ...dependencyStatusCache[name] }; +} + +export function setCachedDependencyStatus(name: DependencyName, status: Partial): void { + dependencyStatusCache[name] = { + ...dependencyStatusCache[name], + ...status, + }; +} + +export function getCachedMediaFolderStatus(downloadPath: string, folderName: string): CachedMediaFolderStatus | null { + return mediaFolderStatusCache.get(getMediaFolderStatusKey(downloadPath, folderName)) ?? null; +} + +export function setCachedMediaFolderStatus(downloadPath: string, folderName: string, status: CachedMediaFolderStatus): void { + mediaFolderStatusCache.set(getMediaFolderStatusKey(downloadPath, folderName), status); +} diff --git a/frontend/src/lib/settings.ts b/frontend/src/lib/settings.ts index f0148f7..8995b32 100644 --- a/frontend/src/lib/settings.ts +++ b/frontend/src/lib/settings.ts @@ -1,14 +1,52 @@ import { GetDefaults } from "../../wailsjs/go/main/App"; -export type FontFamily = "google-sans" | "inter" | "poppins" | "roboto" | "dm-sans" | "plus-jakarta-sans" | "manrope" | "space-grotesk" | "noto-sans" | "nunito-sans" | "figtree" | "raleway" | "public-sans" | "outfit" | "jetbrains-mono" | "geist-sans"; + +export type BuiltInFontFamily = + | "google-sans" + | "inter" + | "poppins" + | "roboto" + | "dm-sans" + | "plus-jakarta-sans" + | "manrope" + | "space-grotesk" + | "noto-sans" + | "nunito-sans" + | "figtree" + | "raleway" + | "public-sans" + | "outfit" + | "jetbrains-mono" + | "geist-sans" + | "bricolage-grotesque"; + +export type CustomFontFamily = `custom-${string}`; +export type FontFamily = BuiltInFontFamily | CustomFontFamily; + +export interface CustomFontOption { + value: CustomFontFamily; + label: string; + fontFamily: string; + url: string; +} + +export interface FontOption { + value: FontFamily; + label: string; + fontFamily: string; + url?: string; +} + export type GifQuality = "fast" | "better"; export type GifResolution = "original" | "high" | "medium" | "low"; export type FetchMode = "single" | "batch"; export type MediaType = "all" | "image" | "video" | "gif" | "text"; + export interface Settings { downloadPath: string; theme: string; themeMode: "auto" | "light" | "dark"; fontFamily: FontFamily; + customFonts: CustomFontOption[]; sfxEnabled: boolean; gifQuality: GifQuality; gifResolution: GifResolution; @@ -18,11 +56,13 @@ export interface Settings { mediaType: MediaType; includeRetweets: boolean; } + export const DEFAULT_SETTINGS: Settings = { downloadPath: "", theme: "yellow", themeMode: "auto", fontFamily: "google-sans", + customFonts: [], sfxEnabled: true, gifQuality: "fast", gifResolution: "original", @@ -32,35 +72,271 @@ export const DEFAULT_SETTINGS: Settings = { mediaType: "all", includeRetweets: false, }; -export const FONT_OPTIONS: { - value: FontFamily; - label: string; - fontFamily: string; -}[] = [ - { value: "dm-sans", label: "DM Sans", fontFamily: '"DM Sans", system-ui, sans-serif' }, - { value: "figtree", label: "Figtree", fontFamily: '"Figtree", system-ui, sans-serif' }, - { value: "geist-sans", label: "Geist Sans", fontFamily: '"Geist", system-ui, sans-serif' }, - { value: "google-sans", label: "Google Sans Flex", fontFamily: '"Google Sans Flex", system-ui, sans-serif' }, - { value: "inter", label: "Inter", fontFamily: '"Inter", system-ui, sans-serif' }, - { value: "jetbrains-mono", label: "JetBrains Mono", fontFamily: '"JetBrains Mono", ui-monospace, monospace' }, - { value: "manrope", label: "Manrope", fontFamily: '"Manrope", system-ui, sans-serif' }, - { value: "noto-sans", label: "Noto Sans", fontFamily: '"Noto Sans", system-ui, sans-serif' }, - { value: "nunito-sans", label: "Nunito Sans", fontFamily: '"Nunito Sans", system-ui, sans-serif' }, - { value: "outfit", label: "Outfit", fontFamily: '"Outfit", system-ui, sans-serif' }, - { value: "plus-jakarta-sans", label: "Plus Jakarta Sans", fontFamily: '"Plus Jakarta Sans", system-ui, sans-serif' }, - { value: "poppins", label: "Poppins", fontFamily: '"Poppins", system-ui, sans-serif' }, - { value: "public-sans", label: "Public Sans", fontFamily: '"Public Sans", system-ui, sans-serif' }, - { value: "raleway", label: "Raleway", fontFamily: '"Raleway", system-ui, sans-serif' }, - { value: "roboto", label: "Roboto", fontFamily: '"Roboto", system-ui, sans-serif' }, - { value: "space-grotesk", label: "Space Grotesk", fontFamily: '"Space Grotesk", system-ui, sans-serif' }, + +export const FONT_OPTIONS: FontOption[] = [ + { + value: "bricolage-grotesque", + label: "Bricolage Grotesque", + fontFamily: '"Bricolage Grotesque", system-ui, sans-serif', + }, + { + value: "dm-sans", + label: "DM Sans", + fontFamily: '"DM Sans", system-ui, sans-serif', + }, + { + value: "figtree", + label: "Figtree", + fontFamily: '"Figtree", system-ui, sans-serif', + }, + { + value: "geist-sans", + label: "Geist Sans", + fontFamily: '"Geist", system-ui, sans-serif', + }, + { + value: "google-sans", + label: "Google Sans", + fontFamily: '"Google Sans Flex", system-ui, sans-serif', + }, + { + value: "inter", + label: "Inter", + fontFamily: '"Inter", system-ui, sans-serif', + }, + { + value: "jetbrains-mono", + label: "JetBrains Mono", + fontFamily: '"JetBrains Mono", ui-monospace, monospace', + }, + { + value: "manrope", + label: "Manrope", + fontFamily: '"Manrope", system-ui, sans-serif', + }, + { + value: "noto-sans", + label: "Noto Sans", + fontFamily: '"Noto Sans", system-ui, sans-serif', + }, + { + value: "nunito-sans", + label: "Nunito Sans", + fontFamily: '"Nunito Sans", system-ui, sans-serif', + }, + { + value: "outfit", + label: "Outfit", + fontFamily: '"Outfit", system-ui, sans-serif', + }, + { + value: "plus-jakarta-sans", + label: "Plus Jakarta Sans", + fontFamily: '"Plus Jakarta Sans", system-ui, sans-serif', + }, + { + value: "poppins", + label: "Poppins", + fontFamily: '"Poppins", system-ui, sans-serif', + }, + { + value: "public-sans", + label: "Public Sans", + fontFamily: '"Public Sans", system-ui, sans-serif', + }, + { + value: "raleway", + label: "Raleway", + fontFamily: '"Raleway", system-ui, sans-serif', + }, + { + value: "roboto", + label: "Roboto", + fontFamily: '"Roboto", system-ui, sans-serif', + }, + { + value: "space-grotesk", + label: "Space Grotesk", + fontFamily: '"Space Grotesk", system-ui, sans-serif', + }, ]; -export function applyFont(fontFamily: FontFamily): void { - const font = FONT_OPTIONS.find(f => f.value === fontFamily); + +const BUILT_IN_FONT_VALUES = new Set(FONT_OPTIONS.map((font) => font.value)); +const SETTINGS_KEY = "twitter-media-downloader-settings"; +const GOOGLE_FONT_LINK_ID_PREFIX = "twitter-media-custom-font-"; +const GOOGLE_FONTS_CSS_HOST = "fonts.googleapis.com"; +const GOOGLE_FONTS_SPECIMEN_HOST = "fonts.google.com"; + +function extractGoogleFontInputUrl(input: string): string { + const trimmed = input.trim(); + const hrefMatch = trimmed.match(/\bhref=["']([^"']+)["']/i); + if (hrefMatch?.[1]) { + return hrefMatch[1]; + } + const importMatch = trimmed.match(/@import\s+url\(["']?([^"')]+)["']?\)/i); + if (importMatch?.[1]) { + return importMatch[1]; + } + return trimmed; +} + +function coerceGoogleFontUrl(rawUrl: string): string { + const trimmed = rawUrl.trim(); + if (/^https?:\/\//i.test(trimmed)) { + return trimmed; + } + if (/^(fonts\.googleapis\.com|fonts\.google\.com)\//i.test(trimmed)) { + return `https://${trimmed}`; + } + return trimmed; +} + +function normalizeFontLabel(label: string): string { + return label.replace(/\+/g, " ").replace(/\s+/g, " ").trim(); +} + +function slugifyFontLabel(label: string): string { + return label.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "font"; +} + +function toFontFamilyCss(label: string): string { + const escapedLabel = label.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); + return `"${escapedLabel}", system-ui, sans-serif`; +} + +function buildGoogleFontsCssUrl(label: string): string { + const url = new URL("https://fonts.googleapis.com/css2"); + url.searchParams.set("family", label); + url.searchParams.set("display", "swap"); + return url.toString(); +} + +function extractSpecimenFontLabel(parsed: URL): string { + const segments = parsed.pathname.split("/").filter(Boolean); + const specimenIndex = segments.findIndex((segment) => segment.toLowerCase() === "specimen"); + const specimenName = specimenIndex >= 0 ? segments[specimenIndex + 1] : ""; + return normalizeFontLabel(decodeURIComponent(specimenName || "")); +} + +function normalizeGoogleFontCssUrl(rawUrl: string): string | null { + try { + const parsed = new URL(coerceGoogleFontUrl(extractGoogleFontInputUrl(rawUrl))); + if (parsed.protocol !== "https:") { + return null; + } + if (parsed.hostname === GOOGLE_FONTS_SPECIMEN_HOST) { + const label = extractSpecimenFontLabel(parsed); + return label ? buildGoogleFontsCssUrl(label) : null; + } + if (parsed.hostname !== GOOGLE_FONTS_CSS_HOST || (parsed.pathname !== "/css" && parsed.pathname !== "/css2")) { + return null; + } + if (parsed.searchParams.getAll("family").length === 0) { + return null; + } + if (!parsed.searchParams.has("display")) { + parsed.searchParams.set("display", "swap"); + } + return parsed.toString(); + } + catch { + return null; + } +} + +export function parseGoogleFontUrl(rawUrl: string): CustomFontOption | null { + const normalizedUrl = normalizeGoogleFontCssUrl(rawUrl); + if (!normalizedUrl) { + return null; + } + const parsed = new URL(normalizedUrl); + const family = parsed.searchParams.getAll("family")[0]; + const label = normalizeFontLabel((family || "").split(":")[0] || ""); + if (!label) { + return null; + } + return { + value: `custom-${slugifyFontLabel(label)}` as CustomFontFamily, + label, + fontFamily: toFontFamilyCss(label), + url: normalizedUrl, + }; +} + +function normalizeCustomFonts(customFonts: unknown): CustomFontOption[] { + if (!Array.isArray(customFonts)) { + return []; + } + const normalizedFonts: CustomFontOption[] = []; + const seenValues = new Set(); + const seenUrls = new Set(); + for (const item of customFonts) { + if (!item || typeof item !== "object") { + continue; + } + const rawUrl = (item as { url?: unknown }).url; + if (typeof rawUrl !== "string") { + continue; + } + const parsed = parseGoogleFontUrl(rawUrl); + if (!parsed || seenValues.has(parsed.value) || seenUrls.has(parsed.url)) { + continue; + } + seenValues.add(parsed.value); + seenUrls.add(parsed.url); + normalizedFonts.push(parsed); + } + return normalizedFonts; +} + +function normalizeFontFamily(fontFamily: unknown, customFonts: CustomFontOption[]): FontFamily { + if (typeof fontFamily !== "string") { + return DEFAULT_SETTINGS.fontFamily; + } + if (BUILT_IN_FONT_VALUES.has(fontFamily as BuiltInFontFamily)) { + return fontFamily as BuiltInFontFamily; + } + const customFont = customFonts.find((font) => font.value === fontFamily); + return customFont ? customFont.value : DEFAULT_SETTINGS.fontFamily; +} + +export function getFontOptions(customFonts: CustomFontOption[] = []): FontOption[] { + return [...FONT_OPTIONS, ...normalizeCustomFonts(customFonts)]; +} + +export function loadGoogleFontUrl(url: string, id = `${GOOGLE_FONT_LINK_ID_PREFIX}preview`): void { + const normalizedUrl = normalizeGoogleFontCssUrl(url); + if (!normalizedUrl) { + return; + } + let link = document.getElementById(id) as HTMLLinkElement | null; + if (!link) { + link = document.createElement("link"); + link.id = id; + link.rel = "stylesheet"; + document.head.appendChild(link); + } + if (link.href !== normalizedUrl) { + link.href = normalizedUrl; + } +} + +function loadCustomFontStylesheets(customFonts: CustomFontOption[]): void { + for (const font of normalizeCustomFonts(customFonts)) { + loadGoogleFontUrl(font.url, `${GOOGLE_FONT_LINK_ID_PREFIX}${font.value}`); + } +} + +export function applyFont(fontFamily: FontFamily, customFonts: CustomFontOption[] = []): void { + const fontOptions = getFontOptions(customFonts); + loadCustomFontStylesheets(customFonts); + const font = fontOptions.find((option) => option.value === fontFamily) || FONT_OPTIONS.find((option) => option.value === DEFAULT_SETTINGS.fontFamily); if (font) { - document.documentElement.style.setProperty('--font-sans', font.fontFamily); + document.documentElement.style.setProperty("--font-sans", font.fontFamily); document.body.style.fontFamily = font.fontFamily; } } + async function fetchDefaultPath(): Promise { try { const data = await GetDefaults(); @@ -71,13 +347,22 @@ async function fetchDefaultPath(): Promise { return ""; } } -const SETTINGS_KEY = "twitter-media-downloader-settings"; + +function toNormalizedSettings(settings: Partial): Settings { + const customFonts = normalizeCustomFonts(settings.customFonts); + return { + ...DEFAULT_SETTINGS, + ...settings, + customFonts, + fontFamily: normalizeFontFamily(settings.fontFamily, customFonts), + }; +} + export function getSettings(): Settings { try { const stored = localStorage.getItem(SETTINGS_KEY); if (stored) { - const parsed = JSON.parse(stored); - return { ...DEFAULT_SETTINGS, ...parsed }; + return toNormalizedSettings(JSON.parse(stored) as Partial); } } catch (error) { @@ -85,36 +370,57 @@ export function getSettings(): Settings { } return DEFAULT_SETTINGS; } + export async function getSettingsWithDefaults(): Promise { const settings = getSettings(); if (!settings.downloadPath) { settings.downloadPath = await fetchDefaultPath(); } - return settings; + return toNormalizedSettings(settings); } + export function saveSettings(settings: Settings): void { try { - localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings)); + const normalizedSettings = toNormalizedSettings(settings); + localStorage.setItem(SETTINGS_KEY, JSON.stringify(normalizedSettings)); } catch (error) { console.error("Failed to save settings:", error); } } + export function updateSettings(partial: Partial): Settings { const current = getSettings(); - const updated = { ...current, ...partial }; + const updated = toNormalizedSettings({ ...current, ...partial }); saveSettings(updated); return updated; } + export async function resetToDefaultSettings(): Promise { const defaultPath = await fetchDefaultPath(); - const defaultSettings = { + const defaultSettings = toNormalizedSettings({ ...DEFAULT_SETTINGS, downloadPath: defaultPath, - }; + }); saveSettings(defaultSettings); return defaultSettings; } + +export async function loadCustomFonts(): Promise { + return normalizeCustomFonts(getSettings().customFonts); +} + +export async function saveCustomFonts(customFonts: CustomFontOption[]): Promise { + const normalizedFonts = normalizeCustomFonts(customFonts); + const settings = getSettings(); + saveSettings({ + ...settings, + customFonts: normalizedFonts, + fontFamily: normalizeFontFamily(settings.fontFamily, normalizedFonts), + }); + return normalizedFonts; +} + export function applyThemeMode(mode: "auto" | "light" | "dark"): void { if (mode === "auto") { const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; diff --git a/frontend/src/lib/toast-with-sound.ts b/frontend/src/lib/toast-with-sound.ts index 1fcb30b..b042565 100644 --- a/frontend/src/lib/toast-with-sound.ts +++ b/frontend/src/lib/toast-with-sound.ts @@ -1,4 +1,4 @@ -import { toast } from "sonner"; +import { toast, type ExternalToast } from "sonner"; import { playSuccessSound, playErrorSound, playWarningSound, playInfoSound, } from "./audio"; import { logger } from "./logger"; import { getSettings } from "./settings"; @@ -7,35 +7,35 @@ const toastStyle = { }; const isSfxEnabled = () => getSettings().sfxEnabled; export const toastWithSound = { - success: (message: string, data?: any) => { + success: (message: string, data?: ExternalToast) => { const msg = message.toLowerCase(); logger.success(msg); if (isSfxEnabled()) playSuccessSound(); return toast.success(msg, { ...toastStyle, ...data }); }, - error: (message: string, data?: any) => { + error: (message: string, data?: ExternalToast) => { const msg = message.toLowerCase(); logger.error(msg); if (isSfxEnabled()) playErrorSound(); return toast.error(msg, { ...toastStyle, ...data }); }, - warning: (message: string, data?: any) => { + warning: (message: string, data?: ExternalToast) => { const msg = message.toLowerCase(); logger.warning(msg); if (isSfxEnabled()) playWarningSound(); return toast.warning(msg, { ...toastStyle, ...data }); }, - info: (message: string, data?: any) => { + info: (message: string, data?: ExternalToast) => { const msg = message.toLowerCase(); logger.info(msg); if (isSfxEnabled()) playInfoSound(); return toast.info(msg, { ...toastStyle, ...data }); }, - message: (message: string, data?: any) => { + message: (message: string, data?: ExternalToast) => { const msg = message.toLowerCase(); logger.info(msg); if (isSfxEnabled()) diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index 5aab430..4963b58 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -32,7 +32,7 @@ export function openExternal(url: string) { try { BrowserOpenURL(url); } - catch (error) { + catch { if (typeof window !== "undefined") { window.open(url, "_blank", "noopener,noreferrer"); } diff --git a/frontend/src/lib/version.ts b/frontend/src/lib/version.ts new file mode 100644 index 0000000..52ace17 --- /dev/null +++ b/frontend/src/lib/version.ts @@ -0,0 +1,22 @@ +export function compareVersionNumbers(left: string, right: string) { + const toParts = (value: string) => value + .trim() + .replace(/^[^\d]*/, "") + .split(/[^0-9]+/) + .filter(Boolean) + .map((part) => Number.parseInt(part, 10)); + const leftParts = toParts(left); + const rightParts = toParts(right); + const maxLength = Math.max(leftParts.length, rightParts.length); + for (let index = 0; index < maxLength; index += 1) { + const leftPart = leftParts[index] ?? 0; + const rightPart = rightParts[index] ?? 0; + if (leftPart > rightPart) { + return 1; + } + if (leftPart < rightPart) { + return -1; + } + } + return 0; +} diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index a16ba49..0eebb75 100644 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -5,6 +5,8 @@ import {backend} from '../models'; export function CheckFolderExists(arg1:string,arg2:string):Promise; +export function CheckFoldersExist(arg1:string,arg2:Array):Promise>; + export function CheckGifsFolderExists(arg1:string,arg2:string):Promise; export function CheckGifsFolderHasMP4(arg1:string,arg2:string):Promise; @@ -13,6 +15,8 @@ export function CleanupExtractorProcesses():Promise; export function ClearAllAccountsFromDB():Promise; +export function ClearStoredAuthToken(arg1:string):Promise; + export function ConvertGIFs(arg1:main.ConvertGIFsRequest):Promise; export function DeleteAccountFromDB(arg1:number):Promise; @@ -43,10 +47,20 @@ export function GetAllGroups():Promise>>; export function GetDefaults():Promise>; +export function GetExifToolVersionStatus():Promise; + +export function GetExtractorVersionStatus():Promise; + +export function GetFFmpegVersionStatus():Promise; + export function GetFolderPath(arg1:string,arg2:string):Promise; export function GetGifsFolderPath(arg1:string,arg2:string):Promise; +export function GetSavedAccountFromDB(arg1:string,arg2:string):Promise; + +export function GetStoredAuthToken(arg1:string):Promise; + export function ImportAccountFromJSON():Promise; export function IsExifToolInstalled():Promise; @@ -65,6 +79,8 @@ export function SaveAccountToDBWithStatus(arg1:string,arg2:string,arg3:string,ar export function SelectFolder(arg1:string):Promise; +export function SetStoredAuthToken(arg1:string,arg2:string):Promise; + export function StopDownload():Promise; export function UpdateAccountGroup(arg1:number,arg2:string,arg3:string):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index aa814c0..ce1c92d 100644 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -6,6 +6,10 @@ export function CheckFolderExists(arg1, arg2) { return window['go']['main']['App']['CheckFolderExists'](arg1, arg2); } +export function CheckFoldersExist(arg1, arg2) { + return window['go']['main']['App']['CheckFoldersExist'](arg1, arg2); +} + export function CheckGifsFolderExists(arg1, arg2) { return window['go']['main']['App']['CheckGifsFolderExists'](arg1, arg2); } @@ -22,6 +26,10 @@ export function ClearAllAccountsFromDB() { return window['go']['main']['App']['ClearAllAccountsFromDB'](); } +export function ClearStoredAuthToken(arg1) { + return window['go']['main']['App']['ClearStoredAuthToken'](arg1); +} + export function ConvertGIFs(arg1) { return window['go']['main']['App']['ConvertGIFs'](arg1); } @@ -82,6 +90,18 @@ export function GetDefaults() { return window['go']['main']['App']['GetDefaults'](); } +export function GetExifToolVersionStatus() { + return window['go']['main']['App']['GetExifToolVersionStatus'](); +} + +export function GetExtractorVersionStatus() { + return window['go']['main']['App']['GetExtractorVersionStatus'](); +} + +export function GetFFmpegVersionStatus() { + return window['go']['main']['App']['GetFFmpegVersionStatus'](); +} + export function GetFolderPath(arg1, arg2) { return window['go']['main']['App']['GetFolderPath'](arg1, arg2); } @@ -90,6 +110,14 @@ export function GetGifsFolderPath(arg1, arg2) { return window['go']['main']['App']['GetGifsFolderPath'](arg1, arg2); } +export function GetSavedAccountFromDB(arg1, arg2) { + return window['go']['main']['App']['GetSavedAccountFromDB'](arg1, arg2); +} + +export function GetStoredAuthToken(arg1) { + return window['go']['main']['App']['GetStoredAuthToken'](arg1); +} + export function ImportAccountFromJSON() { return window['go']['main']['App']['ImportAccountFromJSON'](); } @@ -126,6 +154,10 @@ export function SelectFolder(arg1) { return window['go']['main']['App']['SelectFolder'](arg1); } +export function SetStoredAuthToken(arg1, arg2) { + return window['go']['main']['App']['SetStoredAuthToken'](arg1, arg2); +} + export function StopDownload() { return window['go']['main']['App']['StopDownload'](); } diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index 50c7275..ee228d2 100644 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -36,6 +36,38 @@ export namespace backend { this.statuses_count = source["statuses_count"]; } } + export class DependencyVersionStatus { + installed: boolean; + installed_version?: string; + latest_version?: string; + + static createFrom(source: any = {}) { + return new DependencyVersionStatus(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.installed = source["installed"]; + this.installed_version = source["installed_version"]; + this.latest_version = source["latest_version"]; + } + } + export class ExtractorVersionStatus { + installed: boolean; + installed_version?: string; + latest_version?: string; + + static createFrom(source: any = {}) { + return new ExtractorVersionStatus(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.installed = source["installed"]; + this.installed_version = source["installed_version"]; + this.latest_version = source["latest_version"]; + } + } } @@ -119,6 +151,7 @@ export namespace main { } export class DownloadMediaResponse { success: boolean; + cancelled: boolean; downloaded: number; skipped: number; failed: number; @@ -131,6 +164,7 @@ export namespace main { constructor(source: any = {}) { if ('string' === typeof source) source = JSON.parse(source); this.success = source["success"]; + this.cancelled = source["cancelled"]; this.downloaded = source["downloaded"]; this.skipped = source["skipped"]; this.failed = source["failed"]; diff --git a/go.mod b/go.mod index 304c5f2..063a353 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,18 @@ module twitterxmediabatchdownloader go 1.25.4 require ( - github.com/ulikunitz/xz v0.5.15 github.com/wailsapp/wails/v2 v2.12.0 + github.com/zalando/go-keyring v0.2.8 modernc.org/sqlite v1.50.1 ) require ( git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3 // indirect github.com/bep/debounce v1.2.1 // indirect + github.com/danieljoos/wincred v1.2.3 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ole/go-ole v1.3.0 // indirect - github.com/godbus/dbus/v5 v5.2.0 // indirect + github.com/godbus/dbus/v5 v5.2.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/jchv/go-winloader v0.0.0-20250406163304-c1995be93bd1 // indirect diff --git a/go.sum b/go.sum index ce1842e..06fa828 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,16 @@ git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3 h1:N3IGoHHp9pb6mj1cbXbuaSXV/UMKwmbKLf git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3/go.mod h1:QtOLZGz8olr4qH2vWK0QH0w0O4T9fEIjMuWpKUsH7nc= github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY= github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= +github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ= +github.com/danieljoos/wincred v1.2.3/go.mod h1:6qqX0WNrS4RzPZ1tnroDzq9kY3fu1KwE7MRLQK4X0bs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= -github.com/godbus/dbus/v5 v5.2.0 h1:3WexO+U+yg9T70v9FdHr9kCxYlazaAXUhx2VMkbfax8= -github.com/godbus/dbus/v5 v5.2.0/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= +github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ= +github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -56,12 +58,12 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw= github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tkrajina/go-reflector v0.5.8 h1:yPADHrwmUbMq4RGEyaOUpz2H90sRsETNVpjzo3DLVQQ= github.com/tkrajina/go-reflector v0.5.8/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4= -github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY= -github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= @@ -72,6 +74,8 @@ github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhw github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o= github.com/wailsapp/wails/v2 v2.12.0 h1:BHO/kLNWFHYjCzucxbzAYZWUjub1Tvb4cSguQozHn5c= github.com/wailsapp/wails/v2 v2.12.0/go.mod h1:mo1bzK1DEJrobt7YrBjgxvb5Sihb1mhAY09hppbibQg= +github.com/zalando/go-keyring v0.2.8 h1:6sD/Ucpl7jNq10rM2pgqTs0sZ9V3qMrqfIIy5YPccHs= +github.com/zalando/go-keyring v0.2.8/go.mod h1:tsMo+VpRq5NGyKfxoBVjCuMrG47yj8cmakZDO5QGii0= golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q= golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4= golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= diff --git a/wails.json b/wails.json index 0ce8971..302394e 100644 --- a/wails.json +++ b/wails.json @@ -11,7 +11,7 @@ }, "info": { "productName": "Twitter/X Media Batch Downloader", - "productVersion": "4.4" + "productVersion": "4.5" }, "wailsjsdir": "./frontend", "assetdir": "./frontend/dist",