From 76acb6f5e1de9197811e2f3d8b8d63014fc0250e Mon Sep 17 00:00:00 2001 From: afkarxyz Date: Sun, 5 Jul 2026 19:09:54 +0700 Subject: [PATCH] v4.8 --- app.go | 14 +- backend/download.go | 142 +++++- backend/ffmpeg.go | 13 +- backend/twitter.go | 3 + frontend/package.json.md5 | 2 +- frontend/src/App.tsx | 3 + frontend/src/components/DatabaseView.tsx | 93 ++-- .../src/components/DependencySetupDialog.tsx | 69 ++- frontend/src/components/MediaList.tsx | 413 +++++------------- frontend/src/components/SettingsPage.tsx | 10 +- frontend/src/lib/download-state.ts | 131 ++++++ frontend/src/lib/settings.ts | 77 +++- frontend/src/types/api.ts | 1 + frontend/wailsjs/go/models.ts | 8 + wails.json | 2 +- 15 files changed, 532 insertions(+), 449 deletions(-) create mode 100644 frontend/src/lib/download-state.ts diff --git a/app.go b/app.go index 0320e39..85f1654 100644 --- a/app.go +++ b/app.go @@ -243,6 +243,7 @@ type MediaItemRequest struct { Content string `json:"content,omitempty"` OriginalFilename string `json:"original_filename,omitempty"` AuthorUsername string `json:"author_username,omitempty"` + AccountName string `json:"account_name,omitempty"` } type DownloadMediaWithMetadataRequest struct { @@ -256,6 +257,9 @@ type DownloadMediaWithMetadataRequest struct { Proxy string `json:"proxy,omitempty"` FilenameTemplate string `json:"filename_template,omitempty"` FolderTemplate string `json:"folder_template,omitempty"` + AutoConvertGIFs bool `json:"auto_convert_gifs,omitempty"` + GIFQuality string `json:"gif_quality,omitempty"` + GIFResolution string `json:"gif_resolution,omitempty"` } type DownloadMediaResponse struct { @@ -350,9 +354,9 @@ func (a *App) DownloadMediaWithMetadata(req DownloadMediaWithMetadataRequest) (D } username := req.Username -if (req.Username == "bookmarks" || req.Username == "likes") && item.AuthorUsername != "" { - username = item.AuthorUsername -} + if (req.Username == "bookmarks" || req.Username == "likes") && item.AuthorUsername != "" { + username = item.AuthorUsername + } items[i] = backend.MediaItem{ URL: item.URL, @@ -360,6 +364,7 @@ if (req.Username == "bookmarks" || req.Username == "likes") && item.AuthorUserna TweetID: int64(item.TweetID), Type: item.Type, Username: username, + AccountName: item.AccountName, Content: item.Content, OriginalFilename: originalFilename, } @@ -402,6 +407,9 @@ if (req.Username == "bookmarks" || req.Username == "likes") && item.AuthorUserna RetryAttempts: req.RetryAttempts, FilenameTemplate: req.FilenameTemplate, FolderTemplate: req.FolderTemplate, + AutoConvertGIFs: req.AutoConvertGIFs, + GIFQuality: req.GIFQuality, + GIFResolution: req.GIFResolution, } downloaded, skipped, failed, err := backend.DownloadMediaWithMetadataProgressAndStatus( diff --git a/backend/download.go b/backend/download.go index f6b363d..e832c12 100644 --- a/backend/download.go +++ b/backend/download.go @@ -49,6 +49,9 @@ type DownloadOptions struct { RetryAttempts int FilenameTemplate string FolderTemplate string + AutoConvertGIFs bool + GIFQuality string + GIFResolution string } func NormalizeDownloadOptions(options DownloadOptions) DownloadOptions { @@ -63,6 +66,7 @@ type MediaItem struct { TweetID int64 `json:"tweet_id"` Type string `json:"type"` Username string `json:"username"` + AccountName string `json:"account_name,omitempty"` Content string `json:"content,omitempty"` OriginalFilename string `json:"original_filename,omitempty"` } @@ -247,6 +251,13 @@ type downloadTask struct { index int } +type pendingDownloadTask struct { + task downloadTask + typeDir string + baseName string + ext string +} + func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir string, username string, progress ProgressCallback, itemStatus ItemStatusCallback, ctx context.Context, options DownloadOptions, customProxy string) (downloaded int, skipped int, failed int, err error) { if ctx == nil { ctx = context.Background() @@ -259,7 +270,8 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str } tweetMediaCount := make(map[string]map[int64]int) - tasks := make([]downloadTask, 0, total) + pendingTasks := make([]pendingDownloadTask, 0, total) + filenameCounts := make(map[string]int) downloadDate := time.Now().Format("20060102") for i, item := range items { @@ -268,6 +280,10 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str if itemUsername == "" { itemUsername = username } + itemAccountName := item.AccountName + if itemAccountName == "" { + itemAccountName = itemUsername + } if tweetMediaCount[itemUsername] == nil { tweetMediaCount[itemUsername] = make(map[int64]int) @@ -287,7 +303,7 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str subfolder = "other" } - baseDir := filepath.Join(outputDir, renderFolder(options.FolderTemplate, itemUsername, downloadDate)) + baseDir := filepath.Join(outputDir, renderFolder(options.FolderTemplate, itemUsername, itemAccountName, downloadDate)) if err := os.MkdirAll(baseDir, 0755); err != nil { continue } @@ -309,20 +325,34 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str mediaID = ExtractOriginalFilename(item.URL) } - filename := renderFilename(options.FilenameTemplate, itemUsername, timestamp, item.TweetID, item.Type, mediaIndex, mediaID) + ext - outputPath := filepath.Join(typeDir, filename) + baseName := renderFilename(options.FilenameTemplate, itemUsername, itemAccountName, timestamp, item.TweetID, item.Type, mediaIndex, mediaID) + filenameCounts[filenameCollisionKey(typeDir, baseName, ext)]++ - tasks = append(tasks, downloadTask{ - item: item, - outputPath: outputPath, - index: i, + pendingTasks = append(pendingTasks, pendingDownloadTask{ + task: downloadTask{ + item: item, + index: i, + }, + typeDir: typeDir, + baseName: baseName, + ext: ext, }) } + filenameIndexes := make(map[string]int, len(filenameCounts)) + tasks := make([]downloadTask, 0, len(pendingTasks)) + for _, pending := range pendingTasks { + key := filenameCollisionKey(pending.typeDir, pending.baseName, pending.ext) + filenameIndexes[key]++ + pending.task.outputPath = filepath.Join(pending.typeDir, formatDownloadFilename(pending.baseName, pending.ext, filenameCounts[key], filenameIndexes[key])) + tasks = append(tasks, pending.task) + } + var downloadedCount int64 var skippedCount int64 var failedCount int64 var completedCount int64 + var conversionWG sync.WaitGroup taskChan := make(chan downloadTask, len(tasks)) var wg sync.WaitGroup @@ -421,6 +451,15 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str atomic.AddInt64(&downloadedCount, 1) status = "success" + + if shouldAutoConvertGIF(task.item.Type, options) && ctx.Err() == nil { + conversionWG.Add(1) + go func(inputPath string) { + defer conversionWG.Done() + outputPath := strings.TrimSuffix(inputPath, filepath.Ext(inputPath)) + ".gif" + _ = ConvertMP4ToGIFWithContext(ctx, inputPath, outputPath, normalizeGIFQuality(options.GIFQuality), normalizeGIFResolution(options.GIFResolution)) + }(task.outputPath) + } } if itemStatus != nil { @@ -444,6 +483,7 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str close(taskChan) wg.Wait() + conversionWG.Wait() if err := ctx.Err(); err != nil { return int(downloadedCount), int(skippedCount), int(failedCount), err @@ -452,6 +492,34 @@ func DownloadMediaWithMetadataProgressAndStatus(items []MediaItem, outputDir str return int(downloadedCount), int(skippedCount), int(failedCount), nil } +func shouldAutoConvertGIF(mediaType string, options DownloadOptions) bool { + if !options.AutoConvertGIFs || !IsFFmpegInstalled() { + return false + } + switch mediaType { + case "gif", "animated_gif": + return true + default: + return false + } +} + +func normalizeGIFQuality(quality string) string { + if quality == "better" { + return quality + } + return "fast" +} + +func normalizeGIFResolution(resolution string) string { + switch resolution { + case "original", "high", "medium", "low": + return resolution + default: + return "original" + } +} + func downloadFileWithContext(ctx context.Context, client *http.Client, url, outputPath string, allowOverwrite bool, keepPartialOnFailure bool) error { req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { @@ -527,12 +595,15 @@ func formatTimestamp(dateStr string) string { return "00000000_000000" } -func renderFilename(template, username, timestamp string, tweetID int64, mediaType string, index int, mediaID string) string { +func renderFilename(template, username, accountName, timestamp string, tweetID int64, mediaType string, index int, mediaID string) string { if strings.TrimSpace(template) == "" { - template = "{username}_{date}_{tweet_id}_{index}" + template = "{handle}_{date}_{tweet_id}" } replacer := strings.NewReplacer( + "{handle}", sanitizeFilenamePart(username), + "{name}", sanitizeFilenamePart(accountName), "{username}", sanitizeFilenamePart(username), + "{account_name}", sanitizeFilenamePart(accountName), "{date}", timestamp, "{tweet_id}", fmt.Sprintf("%d", tweetID), "{index}", fmt.Sprintf("%02d", index), @@ -540,25 +611,37 @@ func renderFilename(template, username, timestamp string, tweetID int64, mediaTy "{type}", sanitizeFilenamePart(mediaType), ) name := strings.TrimSpace(replacer.Replace(template)) + name = sanitizeFilenamePart(name) if name == "" { - name = fmt.Sprintf("%s_%s_%d_%02d", username, timestamp, tweetID, index) - } - - if mediaID != "" && !strings.Contains(template, "{media_id}") { - name = name + "_" + sanitizeFilenamePart(mediaID) + name = sanitizeFilenamePart(fmt.Sprintf("%s_%s_%d", username, timestamp, tweetID)) } return name } -func renderFolder(template, username, date string) string { +func filenameCollisionKey(dir, baseName, ext string) string { + return dir + "\x00" + baseName + ext +} + +func formatDownloadFilename(baseName, ext string, duplicateCount, duplicateIndex int) string { + if duplicateCount > 1 { + return fmt.Sprintf("%s_%02d%s", baseName, duplicateIndex, ext) + } + return baseName + ext +} + +func renderFolder(template, username, accountName, date string) string { if strings.TrimSpace(template) == "" { - template = "{username}" + template = "{handle}" } replacer := strings.NewReplacer( + "{handle}", sanitizeFilenamePart(username), + "{name}", sanitizeFilenamePart(accountName), "{username}", sanitizeFilenamePart(username), + "{account_name}", sanitizeFilenamePart(accountName), "{date}", date, ) name := strings.TrimSpace(replacer.Replace(template)) + name = sanitizeFilenamePart(name) if name == "" { name = sanitizeFilenamePart(username) } @@ -566,13 +649,36 @@ func renderFolder(template, username, date string) string { } func sanitizeFilenamePart(s string) string { - return strings.TrimSpace(strings.Map(func(r rune) rune { + cleaned := strings.TrimSpace(strings.Map(func(r rune) rune { switch r { case '\\', '/', ':', '*', '?', '"', '<', '>', '|': return -1 } + if r >= 0 && r < 32 { + return -1 + } return r }, s)) + cleaned = strings.TrimRight(cleaned, ". ") + if isWindowsReservedName(cleaned) { + return cleaned + "_" + } + return cleaned +} + +func isWindowsReservedName(name string) bool { + base := name + if dotIndex := strings.IndexByte(base, '.'); dotIndex >= 0 { + base = base[:dotIndex] + } + base = strings.ToUpper(strings.TrimSpace(base)) + switch base { + case "CON", "PRN", "AUX", "NUL": + return true + } + return len(base) == 4 && + ((strings.HasPrefix(base, "COM") && base[3] >= '1' && base[3] <= '9') || + (strings.HasPrefix(base, "LPT") && base[3] >= '1' && base[3] <= '9')) } func getExtension(mediaURL string, mediaType string) string { diff --git a/backend/ffmpeg.go b/backend/ffmpeg.go index 2362e5a..d261cb2 100644 --- a/backend/ffmpeg.go +++ b/backend/ffmpeg.go @@ -2,6 +2,7 @@ package backend import ( "archive/zip" + "context" "encoding/json" "fmt" "io" @@ -531,11 +532,18 @@ func normalizeFFmpegVersion(version string) string { } func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error { + return ConvertMP4ToGIFWithContext(context.Background(), inputPath, outputPath, quality, resolution) +} + +func ConvertMP4ToGIFWithContext(ctx context.Context, inputPath, outputPath, quality, resolution string) error { ffmpegPath := GetFFmpegPath() if !IsFFmpegInstalled() { return fmt.Errorf("ffmpeg not installed") } + if ctx == nil { + ctx = context.Background() + } var args []string @@ -598,10 +606,13 @@ func ConvertMP4ToGIF(inputPath, outputPath, quality, resolution string) error { } } - cmd := exec.Command(ffmpegPath, args...) + cmd := exec.CommandContext(ctx, ffmpegPath, args...) hideWindow(cmd) output, err := cmd.CombinedOutput() if err != nil { + if ctx.Err() != nil { + return ctx.Err() + } return fmt.Errorf("ffmpeg error: %v, output: %s", err, string(output)) } diff --git a/backend/twitter.go b/backend/twitter.go index d044ed7..a598665 100644 --- a/backend/twitter.go +++ b/backend/twitter.go @@ -272,6 +272,7 @@ type TimelineEntry struct { Verified bool `json:"verified,omitempty"` OriginalFilename string `json:"original_filename,omitempty"` AuthorUsername string `json:"author_username,omitempty"` + AuthorName string `json:"author_name,omitempty"` } type AccountInfo struct { @@ -575,6 +576,7 @@ func textEntry(t richTweet) TimelineEntry { Source: t.Source, Verified: isVerified(t.Author), AuthorUsername: t.Author.Username, + AuthorName: t.Author.Name, } } @@ -608,6 +610,7 @@ func mediaEntry(t richTweet, m richMedia, videoQuality, imageSize string) Timeli Source: t.Source, Verified: isVerified(t.Author), AuthorUsername: t.Author.Username, + AuthorName: t.Author.Name, } } diff --git a/frontend/package.json.md5 b/frontend/package.json.md5 index 854a059..dec6647 100644 --- a/frontend/package.json.md5 +++ b/frontend/package.json.md5 @@ -1 +1 @@ -66d76291490cdae022ca7ad778594a1f \ No newline at end of file +b9015b5860764ef52b634f74fd0f1180 \ No newline at end of file diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 5e5d9a6..760b261 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -9,6 +9,7 @@ import { openExternal } from "@/lib/utils"; import { compareVersionNumbers } from "@/lib/version"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; import { logger } from "@/lib/logger"; +import { initDownloadProgressEvents } from "@/lib/download-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"; @@ -165,10 +166,12 @@ function App() { } }; mediaQuery.addEventListener("change", handleChange); + const cleanupDownloadProgress = initDownloadProgressEvents(); checkForUpdates(); loadHistory(); return () => { mediaQuery.removeEventListener("change", handleChange); + cleanupDownloadProgress(); }; }, []); useEffect(() => { diff --git a/frontend/src/components/DatabaseView.tsx b/frontend/src/components/DatabaseView.tsx index 1698779..ac07b83 100644 --- a/frontend/src/components/DatabaseView.tsx +++ b/frontend/src/components/DatabaseView.tsx @@ -13,16 +13,11 @@ import { Spinner } from "@/components/ui/spinner"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Trash2, FileInput, FileOutput, Pencil, Tag, Shuffle, X, XCircle, Download, StopCircle, Globe, Lock, Bookmark, Heart, Image, Images, Video, Film, FileText, Filter, AlertCircle, MoreVertical, FileBraces, CloudBackup, Search, LayoutGrid, Grid3X3, List, ArrowUpDown, ArrowUp, FolderOpen, Users, MessageSquare } from "lucide-react"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; -import { getSettings } from "@/lib/settings"; +import { getSettings, renderFolderTemplate } from "@/lib/settings"; +import { beginDownload, finishDownload, useDownloadState } from "@/lib/download-state"; import { openExternal } from "@/lib/utils"; import { GetAllAccountsFromDB, GetAccountFromDB, DeleteAccountFromDB, SaveAccountToDB, ExportAccountJSON, ExportAccountsTXT, UpdateAccountGroup, GetAllGroups, DownloadMediaWithMetadata, StopDownload, CheckFoldersExist, OpenFolder, GetFolderPath, } from "../../wailsjs/go/main/App"; -import { EventsOn } from "../../wailsjs/runtime/runtime"; import { main } from "../../wailsjs/go/models"; -interface DownloadProgress { - current: number; - total: number; - percent: number; -} function formatNumberWithComma(num: number): string { return num.toLocaleString(); } @@ -93,23 +88,31 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr const [editGroupName, setEditGroupName] = useState(""); const [editGroupColor, setEditGroupColor] = useState(""); const [clearAllDialogOpen, setClearAllDialogOpen] = useState(false); - const [isDownloading, setIsDownloading] = useState(false); - const [downloadingAccountId, setDownloadingAccountId] = useState(null); - const [downloadProgress, setDownloadProgress] = useState(null); const [isBulkDownloading, setIsBulkDownloading] = useState(false); const [bulkDownloadCurrent, setBulkDownloadCurrent] = useState(0); const [bulkDownloadTotal, setBulkDownloadTotal] = useState(0); const stopBulkDownloadRef = useRef(false); const loadMoreRef = useRef(null); const [folderExistence, setFolderExistence] = useState>(new Map()); - const getFolderNameForAccount = (username: string) => { + const downloadState = useDownloadState(); + const anyDownloadActive = downloadState.active; + const isDownloading = downloadState.active && (downloadState.scope === "database" || downloadState.scope === "database-bulk"); + const downloadingAccountId = isDownloading ? downloadState.accountId : null; + const downloadProgress = isDownloading ? downloadState.progress : null; + const getFolderNameForAccount = (username: string, accountName?: string) => { if (username === "bookmarks") { return "My Bookmarks"; } if (username === "likes") { return "My Likes"; } - return username; + const now = new Date(); + const date = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}`; + return renderFolderTemplate(getSettings().folderTemplate, { + username, + accountName: accountName || username, + date, + }) || username; }; const loadAccounts = useCallback(async () => { setLoading(true); @@ -147,14 +150,6 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr scrollElement.addEventListener("scroll", handleScroll); return () => scrollElement.removeEventListener("scroll", handleScroll); }, []); - useEffect(() => { - const unsubscribe = EventsOn("download-progress", (progress: DownloadProgress) => { - setDownloadProgress(progress); - }); - return () => { - unsubscribe(); - }; - }, []); const scrollToTop = () => { getContentScrollElement()?.scrollTo({ top: 0, behavior: "smooth" }); }; @@ -226,7 +221,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr }, [accountViewMode, filterGroup, filterMediaType, privateAccounts, publicAccounts, deferredSearchQuery, sortOrder]); const visibleAccounts = useMemo(() => filteredAccounts.slice(0, visibleCount), [filteredAccounts, visibleCount]); const visibleAccountsKey = visibleAccounts - .map((account) => `${account.id}:${account.username}`) + .map((account) => `${account.id}:${account.username}:${account.name}`) .join("|"); useEffect(() => { const basePath = currentDownloadPath; @@ -240,7 +235,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr let active = true; const loadVisibleFolderExistence = async () => { try { - const folderNames = visibleAccounts.map((account) => getFolderNameForAccount(account.username)); + const folderNames = visibleAccounts.map((account) => getFolderNameForAccount(account.username, account.name)); const results = await CheckFoldersExist(basePath, folderNames); if (!active) { return; @@ -248,7 +243,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr setFolderExistence((prev) => { const next = new Map(prev); for (const account of visibleAccounts) { - next.set(account.id, results[getFolderNameForAccount(account.username)] ?? false); + next.set(account.id, results[getFolderNameForAccount(account.username, account.name)] ?? false); } return next; }); @@ -316,21 +311,15 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.error("Failed to load account data"); } }; - const handleOpenFolder = async (username: string) => { + const handleOpenFolder = async (account: AccountListItem) => { const settings = getSettings(); - let folderName = username; - if (username === "bookmarks") { - folderName = "My Bookmarks"; - } - else if (username === "likes") { - folderName = "My Likes"; - } + const folderName = getFolderNameForAccount(account.username, account.name); const folderPath = await GetFolderPath(settings.downloadPath, folderName); try { await OpenFolder(folderPath); } catch (error) { - console.error(`Failed to open folder for @${username}:`, error); + console.error(`Failed to open folder for @${account.username}:`, error); toast.error("Failed to open folder"); } }; @@ -356,9 +345,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr outputDir = `${settings.downloadPath}${separator}My Likes`; } const actualUsername = data.account_info?.name || username; - setIsDownloading(true); - setDownloadingAccountId(id); - setDownloadProgress({ current: 0, total: timeline.length, percent: 0 }); + beginDownload({ current: 0, total: timeline.length, percent: 0 }, { scope: "database", accountId: id }); const request = new main.DownloadMediaWithMetadataRequest({ items: timeline.map((item: { url: string; @@ -367,6 +354,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr type: string; original_filename?: string; author_username?: string; + author_name?: string; }) => new main.MediaItemRequest({ url: item.url, date: item.date, @@ -374,6 +362,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr type: item.type, original_filename: item.original_filename || "", author_username: item.author_username || "", + account_name: item.author_name || data.account_info?.nick || actualUsername, })), output_dir: outputDir, username: actualUsername, @@ -384,6 +373,9 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr proxy: settings.proxy || "", filename_template: settings.filenameTemplate || "", folder_template: settings.folderTemplate || "", + auto_convert_gifs: settings.autoConvertGifs, + gif_quality: settings.gifQuality || "fast", + gif_resolution: settings.gifResolution || "original", }); const response = await DownloadMediaWithMetadata(request); if (response.cancelled) { @@ -420,15 +412,14 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.error(`Download failed: ${errorMsg}`); } finally { - setIsDownloading(false); - setDownloadingAccountId(null); - setDownloadProgress(null); + finishDownload(); } }; const handleStopDownload = async () => { try { const stopped = await StopDownload(); if (stopped) { + finishDownload(); toast.info("Download stopped"); } } @@ -460,7 +451,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr if (!account) continue; setBulkDownloadCurrent(i + 1); - setDownloadingAccountId(id); + beginDownload({ current: 0, total: 0, percent: 0 }, { scope: "database-bulk", accountId: id }); try { const responseJSON = await GetAccountFromDB(id); const data = JSON.parse(responseJSON); @@ -471,7 +462,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr toast.info("Bulk download stopped"); break; } - setDownloadProgress({ current: 0, total: timeline.length, percent: 0 }); + beginDownload({ current: 0, total: timeline.length, percent: 0 }, { scope: "database-bulk", accountId: id }); const isBookmarks = data.account_info?.nick === "My Bookmarks" || account.username === "bookmarks"; const isLikes = data.account_info?.nick === "My Likes" || account.username === "likes"; let outputDir = settings.downloadPath; @@ -491,6 +482,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr tweet_id: string; type: string; author_username?: string; + author_name?: string; original_filename?: string; }) => new main.MediaItemRequest({ url: item.url, @@ -498,6 +490,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr tweet_id: item.tweet_id, type: item.type, author_username: item.author_username || "", + account_name: item.author_name || data.account_info?.nick || actualUsername, original_filename: item.original_filename || "", })), output_dir: outputDir, @@ -509,6 +502,9 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr proxy: settings.proxy || "", filename_template: settings.filenameTemplate || "", folder_template: settings.folderTemplate || "", + auto_convert_gifs: settings.autoConvertGifs, + gif_quality: settings.gifQuality || "fast", + gif_resolution: settings.gifResolution || "original", }); const response = await DownloadMediaWithMetadata(request); if (response.cancelled) { @@ -527,8 +523,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr } } setIsBulkDownloading(false); - setDownloadingAccountId(null); - setDownloadProgress(null); + finishDownload(); setBulkDownloadCurrent(0); setBulkDownloadTotal(0); if ((totalDownloaded > 0 || totalSkipped > 0) && !stopBulkDownloadRef.current) { @@ -825,7 +820,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr Stop Bulk Download ) : ( - @@ -1056,10 +1051,10 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr
{downloadingAccountId === account.id ? () : () : ()} - @@ -1150,10 +1145,10 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr
{downloadingAccountId === account.id ? () : () : ()} - @@ -1268,7 +1263,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr Stop Download ) : ( - @@ -1276,7 +1271,7 @@ export function DatabaseView({ onLoadAccount, onUpdateSelected }: DatabaseViewPr )} - diff --git a/frontend/src/components/DependencySetupDialog.tsx b/frontend/src/components/DependencySetupDialog.tsx index 4a55c1e..a94efd2 100644 --- a/frontend/src/components/DependencySetupDialog.tsx +++ b/frontend/src/components/DependencySetupDialog.tsx @@ -4,30 +4,40 @@ 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 { compareVersionNumbers } from "@/lib/version"; import { Quit } from "../../wailsjs/runtime/runtime"; -import { DownloadExtractor, IsExtractorInstalled } from "../../wailsjs/go/main/App"; +import { DownloadExtractor, GetExtractorVersionStatus } from "../../wailsjs/go/main/App"; interface DependencySetupDialogProps { onInstalled?: () => void; } -interface GithubReleaseResponse { - tag_name?: string; +interface ExtractorVersionStatus { + installed: boolean; + installed_version?: string; + latest_version?: 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 [updateRequired, setUpdateRequired] = useState(false); + const [installedVersion, setInstalledVersion] = 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(); + const status = await GetExtractorVersionStatus() as ExtractorVersionStatus; + const nextInstalledVersion = status.installed_version?.trim() || null; + const nextLatestVersion = status.latest_version?.trim() || null; + const nextUpdateRequired = !!(status.installed && nextLatestVersion && (!nextInstalledVersion || compareVersionNumbers(nextLatestVersion, nextInstalledVersion) > 0)); if (!cancelled) { - setOpen(!installed); + setUpdateRequired(nextUpdateRequired); + setInstalledVersion(nextInstalledVersion); + setReleaseVersion(nextLatestVersion); + setReleaseStatusMessage(nextLatestVersion ? `Latest GitHub release: ${nextLatestVersion}` : "Latest GitHub release unavailable."); + setOpen(!status.installed || nextUpdateRequired); setErrorMessage(null); } } @@ -43,39 +53,9 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp } } }; - 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 () => { @@ -84,7 +64,7 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp try { await DownloadExtractor(); setOpen(false); - toast.success("Xtractor installed"); + toast.success(updateRequired ? "Xtractor updated" : "Xtractor installed"); onInstalled?.(); } catch (error) { @@ -114,11 +94,16 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp
- Download Xtractor - Xtractor is required before you can use this app. + {updateRequired ? "Update Xtractor" : "Download Xtractor"} + + {updateRequired ? "A newer Xtractor binary is available or the installed version could not be verified." : "Xtractor is required before you can use this app."} +
+ {updateRequired && releaseVersion && (

+ Installed: {installedVersion || "unknown"} - Latest: {releaseVersion} +

)}

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

{errorMessage && (
@@ -134,10 +119,10 @@ export function DependencySetupDialog({ onInstalled }: DependencySetupDialogProp diff --git a/frontend/src/components/MediaList.tsx b/frontend/src/components/MediaList.tsx index fa5cdcd..8b11304 100644 --- a/frontend/src/components/MediaList.tsx +++ b/frontend/src/components/MediaList.tsx @@ -11,21 +11,11 @@ import type { TimelineEntry, AccountInfo } from "@/types/api"; import { logger } from "@/lib/logger"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; import { getSettings, renderFolderTemplate } from "@/lib/settings"; -import { getCachedDependencyStatus, getCachedMediaFolderStatus, setCachedDependencyStatus, setCachedMediaFolderStatus } from "@/lib/runtime-cache"; +import { beginDownload, finishDownload, setDownloadItemStatus, useDownloadState } from "@/lib/download-state"; +import { getCachedMediaFolderStatus, setCachedMediaFolderStatus } from "@/lib/runtime-cache"; import { openExternal } from "@/lib/utils"; -import { DownloadMediaWithMetadata, DownloadProfileImage, OpenFolder, IsFFmpegInstalled, ConvertGIFs, StopDownload, CheckFolderExists, CheckGifsFolderHasMP4 } from "../../wailsjs/go/main/App"; -import { EventsOn } from "../../wailsjs/runtime/runtime"; +import { DownloadMediaWithMetadata, DownloadProfileImage, OpenFolder, StopDownload, CheckFolderExists } from "../../wailsjs/go/main/App"; import { main } from "../../wailsjs/go/models"; -interface DownloadProgress { - current: number; - total: number; - percent: number; -} -interface DownloadItemStatus { - tweet_id: number; - index: number; - status: "success" | "failed" | "skipped"; -} interface MediaListProps { accountInfo: AccountInfo; timeline: TimelineEntry[]; @@ -188,6 +178,7 @@ function getAccountFolderName(accountInfo: AccountInfo): string { const now = new Date(); const rendered = renderFolderTemplate(template, { username: accountInfo.name, + accountName: accountInfo.nick, date: `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}`, }); return rendered || accountInfo.name; @@ -206,6 +197,12 @@ function pickVariantUrl(variants: { export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = "all", newMediaCount = null, }: MediaListProps) { const settings = getSettings(); const folderName = getAccountFolderName(accountInfo); + const downloadState = useDownloadState(); + const anyDownloadActive = downloadState.active; + const isDownloading = downloadState.active && downloadState.scope === "media"; + const downloadProgress = isDownloading ? downloadState.progress : null; + const downloadingItem = isDownloading ? downloadState.currentItemKey : null; + const itemStatusByKey = downloadState.itemStatusByKey; const cachedMediaFolderStatus = settings.downloadPath ? getCachedMediaFolderStatus(settings.downloadPath, folderName) : null; @@ -219,19 +216,10 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = useEffect(() => { localStorage.setItem("resultViewMode", viewMode); }, [viewMode]); - const [isDownloading, setIsDownloading] = useState(false); - const [downloadProgress, setDownloadProgress] = useState(null); const [hasDownloaded, setHasDownloaded] = useState(false); - const [isConverting, setIsConverting] = useState(false); - const [ffmpegInstalled, setFfmpegInstalled] = useState(() => getCachedDependencyStatus("ffmpeg").installed ?? false); const [showScrollTop, setShowScrollTop] = 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(MEDIA_LIST_PAGE_SIZE); const loadMoreRef = useRef(null); useEffect(() => { @@ -282,24 +270,19 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = setVisibleCount(MEDIA_LIST_PAGE_SIZE); }, [filteredTimeline.length]); useEffect(() => { - const checkFoldersAndFFmpeg = async () => { + const checkFolders = async () => { const settings = getSettings(); const basePath = settings.downloadPath; - const installed = await IsFFmpegInstalled(); - setCachedDependencyStatus("ffmpeg", { installed }); - setFfmpegInstalled(installed); if (!basePath || !accountInfo.name) return; const exists = await CheckFolderExists(basePath, folderName); - const hasMP4 = exists ? await CheckGifsFolderHasMP4(basePath, folderName) : false; setCachedMediaFolderStatus(basePath, folderName, { folderExists: exists, - gifsFolderHasMP4: hasMP4, + gifsFolderHasMP4: false, }); setFolderExists(exists); - setGifsFolderHasMP4(hasMP4); }; - checkFoldersAndFFmpeg(); + checkFolders(); }, [accountInfo.name, accountInfo.nick, folderName, hasDownloaded]); useEffect(() => { const sentinel = loadMoreRef.current; @@ -340,58 +323,6 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = document.body.style.overflow = previousOverflow; }; }, [previewIndex]); - useEffect(() => { - const unsubscribe = EventsOn("download-progress", (progress: DownloadProgress) => { - setDownloadProgress(progress); - }); - return () => { - unsubscribe(); - }; - }, []); - const currentDownloadingItemKeyRef = useRef(null); - const bulkDownloadKeyMapRef = useRef>(new Map()); - useEffect(() => { - const unsubscribe = EventsOn("download-item-status", (status: DownloadItemStatus) => { - const currentKey = currentDownloadingItemKeyRef.current; - const keyMap = bulkDownloadKeyMapRef.current; - let itemKey: string; - if (currentKey) { - itemKey = currentKey; - } - else if (keyMap.size > 0) { - itemKey = keyMap.get(status.index) ?? `${String(status.tweet_id)}-${status.index}`; - } - else { - itemKey = `${String(status.tweet_id)}-${status.index}`; - } - if (status.status === "success") { - setDownloadedItems((prev) => new Set(prev).add(itemKey)); - if (currentKey) { - toast.success("Downloaded"); - } - } - else if (status.status === "failed") { - setFailedItems((prev) => new Set(prev).add(itemKey)); - if (currentKey) { - toast.error("Download failed"); - } - } - else if (status.status === "skipped") { - setDownloadedItems((prev) => { - const newSet = new Set(prev); - newSet.delete(itemKey); - return newSet; - }); - setSkippedItems((prev) => new Set(prev).add(itemKey)); - if (currentKey) { - toast.info("Already exists"); - } - } - }); - return () => { - unsubscribe(); - }; - }, []); const goToPrevious = () => { if (previewIndex !== null && previewIndex > 0) { setPreviewIndex(previewIndex - 1); @@ -459,6 +390,73 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = } return settings.downloadPath; }; + const createMediaItemRequest = (item: TimelineEntry) => new main.MediaItemRequest({ + url: item.url, + date: item.date, + tweet_id: item.tweet_id, + type: item.type, + content: item.content || "", + original_filename: item.original_filename || "", + author_username: item.author_username || "", + account_name: item.author_name || accountInfo.nick || accountInfo.name, + }); + const getItemStatusFlags = (itemKey: string) => { + const status = itemStatusByKey[itemKey]; + return { + isItemDownloaded: status === "success", + isItemFailed: status === "failed", + isItemSkipped: status === "skipped", + isItemDownloading: downloadingItem === itemKey, + }; + }; + const handleDownloadItem = async (item: TimelineEntry, itemKey: string) => { + beginDownload({ current: 0, total: 1, percent: 0 }, { scope: "media", currentItemKey: itemKey, itemKeyByIndex: { 0: itemKey } }); + try { + const settings = getSettings(); + const request = new main.DownloadMediaWithMetadataRequest({ + items: [createMediaItemRequest(item)], + output_dir: getOutputDir(), + username: accountInfo.name, + concurrent_downloads: settings.concurrentDownloads || 10, + skip_existing: settings.skipExistingFiles, + delete_incomplete_files: settings.deleteIncompleteFiles, + retry_attempts: settings.retryAttempts, + proxy: settings.proxy || "", + filename_template: settings.filenameTemplate || "", + folder_template: settings.folderTemplate || "", + auto_convert_gifs: settings.autoConvertGifs, + gif_quality: settings.gifQuality || "fast", + gif_resolution: settings.gifResolution || "original", + }); + const response = await DownloadMediaWithMetadata(request); + if (response.cancelled) { + toast.info(response.message || "Download stopped"); + } + else if (response.success) { + if (response.skipped > 0) { + setDownloadItemStatus(itemKey, "skipped"); + toast.info("Already exists"); + } + else if (response.downloaded > 0) { + setDownloadItemStatus(itemKey, "success"); + toast.success("Downloaded"); + } + setHasDownloaded(true); + } + else { + setDownloadItemStatus(itemKey, "failed"); + toast.error(response.message || "Download failed"); + } + } + catch (error) { + setDownloadItemStatus(itemKey, "failed"); + const errorMsg = error instanceof Error ? error.message : String(error); + toast.error(`Download failed: ${errorMsg}`); + } + finally { + finishDownload(); + } + }; const handleDownload = async () => { const itemsWithIndices = selectedItems.size > 0 ? filteredTimeline @@ -469,26 +467,16 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = toast.error("No media to download"); return; } - const keyMap = new Map(); + const keyMap: Record = {}; itemsWithIndices.forEach((entry, backendIndex) => { - keyMap.set(backendIndex, getItemKey(entry.item)); + keyMap[backendIndex] = getItemKey(entry.item); }); - bulkDownloadKeyMapRef.current = keyMap; - setIsDownloading(true); - setDownloadProgress({ current: 0, total: itemsWithIndices.length, percent: 0 }); + beginDownload({ current: 0, total: itemsWithIndices.length, percent: 0 }, { scope: "media", itemKeyByIndex: keyMap }); logger.info(`Starting download of ${itemsWithIndices.length} files...`); try { const settings = getSettings(); const request = new main.DownloadMediaWithMetadataRequest({ - items: itemsWithIndices.map(({ item }) => new main.MediaItemRequest({ - url: item.url, - date: item.date, - tweet_id: item.tweet_id, - type: item.type, - content: item.content || "", - original_filename: item.original_filename || "", - author_username: item.author_username || "", - })), + items: itemsWithIndices.map(({ item }) => createMediaItemRequest(item)), output_dir: getOutputDir(), username: accountInfo.name, concurrent_downloads: settings.concurrentDownloads || 10, @@ -498,6 +486,9 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = proxy: settings.proxy || "", filename_template: settings.filenameTemplate || "", folder_template: settings.folderTemplate || "", + auto_convert_gifs: settings.autoConvertGifs, + gif_quality: settings.gifQuality || "fast", + gif_resolution: settings.gifResolution || "original", }); const response = await DownloadMediaWithMetadata(request); if (response.cancelled) { @@ -528,17 +519,12 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = toast.success(message); } 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, + gifsFolderHasMP4: false, }); setFolderExists(true); - setGifsFolderHasMP4(hasMP4); } } else { @@ -552,15 +538,14 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = toast.error("Download failed"); } finally { - setIsDownloading(false); - setDownloadProgress(null); - bulkDownloadKeyMapRef.current = new Map(); + finishDownload(); } }; const handleStopDownload = async () => { try { const stopped = await StopDownload(); if (stopped) { + finishDownload(); logger.info("Download stopped by user"); toast.info("Stopped"); } @@ -607,8 +592,8 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = } else { folderPath = settings.downloadPath - ? `${settings.downloadPath}/${accountInfo.name}` - : accountInfo.name; + ? `${settings.downloadPath}/${folderName}` + : folderName; } try { await OpenFolder(folderPath); @@ -625,52 +610,6 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = const handleOpenTweet = (tweetId: string) => { openExternal(`https://x.com/${accountInfo.name}/status/${tweetId}`); }; - const handleConvertGifs = async () => { - const settings = getSettings(); - const isBookmarks = accountInfo.nick === "My Bookmarks"; - const isLikes = accountInfo.nick === "My Likes"; - let folderPath: string; - if (isBookmarks || isLikes) { - const separator = settings.downloadPath.includes("/") ? "/" : "\\"; - const folderName = isBookmarks ? "My Bookmarks" : "My Likes"; - folderPath = `${settings.downloadPath}${separator}${folderName}`; - } - else { - folderPath = `${settings.downloadPath}/${accountInfo.name}`; - } - setIsConverting(true); - logger.info("Converting GIFs..."); - try { - const response = await ConvertGIFs({ - folder_path: folderPath, - quality: settings.gifQuality || "fast", - resolution: settings.gifResolution || "high", - delete_original: false, - }); - if (response.success) { - logger.success(`Converted ${response.converted} GIFs`); - toast.success(`${response.converted} GIFs converted`); - const hasMP4 = await CheckGifsFolderHasMP4(settings.downloadPath, folderName); - setCachedMediaFolderStatus(settings.downloadPath, folderName, { - folderExists: true, - gifsFolderHasMP4: hasMP4, - }); - setGifsFolderHasMP4(hasMP4); - } - else { - logger.error(response.message); - toast.error("Convert failed"); - } - } - catch (error) { - const errorMsg = error instanceof Error ? error.message : String(error); - logger.error(`Convert failed: ${errorMsg}`); - toast.error("Convert failed"); - } - finally { - setIsConverting(false); - } - }; return (
{accountInfo.name === "bookmarks" || accountInfo.name === "likes" ? (
@@ -819,21 +758,12 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = Open Folder -
{isDownloading && ()} - @@ -976,10 +857,7 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = {filteredTimeline.slice(0, visibleCount).map((item, index) => { const itemKey = getItemKey(item); const isSelected = selectedItems.has(itemKey); - const isItemDownloaded = downloadedItems.has(itemKey); - const isItemFailed = failedItems.has(itemKey); - const isItemSkipped = skippedItems.has(itemKey); - const isItemDownloading = downloadingItem === itemKey; + const { isItemDownloaded, isItemFailed, isItemSkipped, isItemDownloading } = getItemStatusFlags(itemKey); return (
openPreview(index)}> @@ -996,51 +874,8 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = @@ -1169,56 +1004,10 @@ export function MediaList({ accountInfo, timeline, totalUrls, fetchedMediaType = {(() => { const item = filteredTimeline[previewIndex]; const itemKey = getItemKey(item); - const isItemDownloaded = downloadedItems.has(itemKey); - const isItemFailed = failedItems.has(itemKey); - const isItemSkipped = skippedItems.has(itemKey); - const isItemDownloading = downloadingItem === itemKey; + const { isItemDownloaded, isItemFailed, isItemSkipped, isItemDownloading } = getItemStatusFlags(itemKey); return (); diff --git a/frontend/src/components/SettingsPage.tsx b/frontend/src/components/SettingsPage.tsx index 47fd3a7..ddffd7e 100644 --- a/frontend/src/components/SettingsPage.tsx +++ b/frontend/src/components/SettingsPage.tsx @@ -591,14 +591,18 @@ export function SettingsPage({ onUnsavedChangesChange, onResetRequest }: Setting
- + + setTempSettings((prev) => ({ ...prev, autoConvertGifs: checked }))} disabled={!ffmpegInstalled}/> +
+
+
- setTempSettings((prev) => ({ ...prev, gifResolution: value }))} disabled={!ffmpegInstalled || !tempSettings.autoConvertGifs}> diff --git a/frontend/src/lib/download-state.ts b/frontend/src/lib/download-state.ts new file mode 100644 index 0000000..567b886 --- /dev/null +++ b/frontend/src/lib/download-state.ts @@ -0,0 +1,131 @@ +import { useSyncExternalStore } from "react"; +import { EventsOn } from "../../wailsjs/runtime/runtime"; + +export interface DownloadProgress { + current: number; + total: number; + percent: number; +} + +export interface DownloadItemStatus { + tweet_id: number; + index: number; + status: "success" | "failed" | "skipped" | "cancelled"; +} + +export type DownloadScope = "media" | "database" | "database-bulk"; +export type DownloadItemResult = "success" | "failed" | "skipped"; + +export interface DownloadState { + active: boolean; + scope: DownloadScope | null; + accountId: number | null; + progress: DownloadProgress | null; + currentItemKey: string | null; + itemKeyByIndex: Record; + itemStatusByKey: Record; +} + +const listeners = new Set<() => void>(); + +let state: DownloadState = { + active: false, + scope: null, + accountId: null, + progress: null, + currentItemKey: null, + itemKeyByIndex: {}, + itemStatusByKey: {}, +}; + +let unsubscribeEvents: (() => void) | null = null; + +function emit() { + for (const listener of listeners) { + listener(); + } +} + +function setState(next: Partial) { + state = { ...state, ...next }; + emit(); +} + +export function initDownloadProgressEvents() { + if (unsubscribeEvents) { + return unsubscribeEvents; + } + const unsubscribeProgress = EventsOn("download-progress", (progress: DownloadProgress) => { + if (!state.scope) { + return; + } + setState({ progress }); + }); + const unsubscribeItemStatus = EventsOn("download-item-status", (status: DownloadItemStatus) => { + if (status.status === "cancelled") { + return; + } + const itemKey = state.currentItemKey || state.itemKeyByIndex[status.index] || `${String(status.tweet_id)}-${status.index}`; + setState({ + itemStatusByKey: { + ...state.itemStatusByKey, + [itemKey]: status.status, + }, + }); + }); + unsubscribeEvents = () => { + unsubscribeProgress(); + unsubscribeItemStatus(); + unsubscribeEvents = null; + }; + return unsubscribeEvents; +} + +export function beginDownload( + progress: DownloadProgress, + meta: Pick & Partial> +) { + setState({ + active: true, + scope: meta.scope, + accountId: meta.accountId ?? null, + progress, + currentItemKey: meta.currentItemKey ?? null, + itemKeyByIndex: meta.itemKeyByIndex ?? {}, + }); +} + +export function finishDownload() { + setState({ + active: false, + scope: null, + accountId: null, + progress: null, + currentItemKey: null, + itemKeyByIndex: {}, + }); +} + +export function setDownloadItemStatus(itemKey: string, status: DownloadItemResult) { + setState({ + itemStatusByKey: { + ...state.itemStatusByKey, + [itemKey]: status, + }, + }); +} + +export function subscribeDownloadState(listener: () => void) { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; +} + +export function getDownloadState() { + return state; +} + +export function useDownloadState() { + return useSyncExternalStore(subscribeDownloadState, getDownloadState, getDownloadState); +} diff --git a/frontend/src/lib/settings.ts b/frontend/src/lib/settings.ts index 57143d6..ccdfd6c 100644 --- a/frontend/src/lib/settings.ts +++ b/frontend/src/lib/settings.ts @@ -58,67 +58,86 @@ export const BANNER_SIZES: { { value: "600x200", label: "600×200" }, { value: "300x100", label: "300×100" }, ]; -export const DEFAULT_FILENAME_TEMPLATE = "{username}_{date}_{tweet_id}_{index}"; +export const DEFAULT_FILENAME_TEMPLATE = "{handle}_{date}_{tweet_id}"; export interface TemplateToken { key: string; description: string; example: string; } export const FILENAME_TEMPLATE_VARIABLES: TemplateToken[] = [ - { key: "{username}", description: "Author handle", example: "NASA" }, + { key: "{handle}", description: "Author handle", example: "elonmusk" }, + { key: "{name}", description: "Author display name", example: "Elon Musk" }, { key: "{date}", description: "Tweet date & time", example: "20250620_104556" }, { key: "{tweet_id}", description: "Tweet ID", example: "1989033613824815258" }, - { key: "{index}", description: "Media position in tweet", example: "01" }, { key: "{media_id}", description: "Unique media ID (prevents overwrites)", example: "Gx1aB2cXkAA7Yz9" }, { key: "{type}", description: "Media type", example: "photo" }, ]; export interface FilenameTemplateData { username?: string; + accountName?: string; date?: string; tweetId?: string; index?: number; + mediaID?: string; type?: string; } export const SAMPLE_FILENAME_DATA: FilenameTemplateData = { - username: "NASA", + username: "elonmusk", + accountName: "Elon Musk", date: "20250620_104556", tweetId: "1989033613824815258", index: 1, + mediaID: "Gx1aB2cXkAA7Yz9", type: "photo", }; +function sanitizeTemplatePart(input: string): string { + const cleaned = input + .replace(/[\u0000-\u001f\\/:*?"<>|]/g, "") + .trim() + .replace(/[. ]+$/g, ""); + return /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i.test(cleaned) ? `${cleaned}_` : cleaned; +} export function renderFilenameTemplate(template: string, data: FilenameTemplateData): string { if (!template) return ""; - const safe = (s: string) => s.replace(/[\\/:*?"<>|]/g, "").trim(); - return template - .replace(/\{username\}/g, safe(data.username ?? "")) - .replace(/\{date\}/g, safe(data.date ?? "")) - .replace(/\{tweet_id\}/g, safe(data.tweetId ?? "")) + return sanitizeTemplatePart(template + .replace(/\{handle\}/g, sanitizeTemplatePart(data.username ?? "")) + .replace(/\{name\}/g, sanitizeTemplatePart(data.accountName ?? "")) + .replace(/\{username\}/g, sanitizeTemplatePart(data.username ?? "")) + .replace(/\{account_name\}/g, sanitizeTemplatePart(data.accountName ?? "")) + .replace(/\{date\}/g, sanitizeTemplatePart(data.date ?? "")) + .replace(/\{tweet_id\}/g, sanitizeTemplatePart(data.tweetId ?? "")) .replace(/\{index\}/g, String(data.index ?? 1).padStart(2, "0")) - .replace(/\{type\}/g, safe(data.type ?? "")) - .trim(); + .replace(/\{media_id\}/g, sanitizeTemplatePart(data.mediaID ?? "")) + .replace(/\{type\}/g, sanitizeTemplatePart(data.type ?? "")) + .trim()); } -export const DEFAULT_FOLDER_TEMPLATE = "{username}"; +export const DEFAULT_FOLDER_TEMPLATE = "{handle}"; export const FOLDER_TEMPLATE_VARIABLES: TemplateToken[] = [ - { key: "{username}", description: "Author handle", example: "NASA" }, + { key: "{handle}", description: "Author handle", example: "elonmusk" }, + { key: "{name}", description: "Author display name", example: "Elon Musk" }, { key: "{date}", description: "Download date", example: "20250620" }, ]; export interface FolderTemplateData { username?: string; + accountName?: string; date?: string; } export const SAMPLE_FOLDER_DATA: FolderTemplateData = { - username: "NASA", + username: "elonmusk", + accountName: "Elon Musk", date: "20250620", }; export function renderFolderTemplate(template: string, data: FolderTemplateData): string { if (!template) return ""; - const safe = (s: string) => s.replace(/[\\/:*?"<>|]/g, "").trim(); - return template - .replace(/\{username\}/g, safe(data.username ?? "")) - .replace(/\{date\}/g, safe(data.date ?? "")) - .trim(); + return sanitizeTemplatePart(template + .replace(/\{handle\}/g, sanitizeTemplatePart(data.username ?? "")) + .replace(/\{name\}/g, sanitizeTemplatePart(data.accountName ?? "")) + .replace(/\{username\}/g, sanitizeTemplatePart(data.username ?? "")) + .replace(/\{account_name\}/g, sanitizeTemplatePart(data.accountName ?? "")) + .replace(/\{date\}/g, sanitizeTemplatePart(data.date ?? "")) + .trim()); } const DEFAULT_CONCURRENT_DOWNLOADS = 10; const MIN_CONCURRENT_DOWNLOADS = 1; @@ -137,6 +156,7 @@ export interface Settings { fontFamily: FontFamily; customFonts: CustomFontOption[]; sfxEnabled: boolean; + autoConvertGifs: boolean; gifQuality: GifQuality; gifResolution: GifResolution; proxy: string; @@ -164,6 +184,7 @@ export const DEFAULT_SETTINGS: Settings = { fontFamily: "google-sans", customFonts: [], sfxEnabled: true, + autoConvertGifs: false, gifQuality: "fast", gifResolution: "original", proxy: "", @@ -428,6 +449,21 @@ function normalizeRetryAttempts(value: unknown): number { } return rounded; } +function normalizeTemplateTokens(value: unknown, fallback: string): string { + if (typeof value !== "string") { + return fallback; + } + return value + .replace(/\{username\}/g, "{handle}") + .replace(/\{account_name\}/g, "{name}"); +} +function normalizeFilenameTemplate(value: unknown): string { + const template = normalizeTemplateTokens(value, DEFAULT_FILENAME_TEMPLATE); + return template === "{handle}_{date}_{tweet_id}_{index}" ? DEFAULT_FILENAME_TEMPLATE : template; +} +function normalizeFolderTemplate(value: unknown): string { + return normalizeTemplateTokens(value, DEFAULT_FOLDER_TEMPLATE); +} export function getFontOptions(customFonts: CustomFontOption[] = []): FontOption[] { return [...FONT_OPTIONS, ...normalizeCustomFonts(customFonts)]; } @@ -480,6 +516,9 @@ function toNormalizedSettings(settings: Partial): Settings { skipExistingFiles: normalizeBoolean(settings.skipExistingFiles, DEFAULT_SETTINGS.skipExistingFiles), deleteIncompleteFiles: normalizeBoolean(settings.deleteIncompleteFiles, DEFAULT_SETTINGS.deleteIncompleteFiles), retryAttempts: normalizeRetryAttempts(settings.retryAttempts), + autoConvertGifs: normalizeBoolean(settings.autoConvertGifs, DEFAULT_SETTINGS.autoConvertGifs), + filenameTemplate: normalizeFilenameTemplate(settings.filenameTemplate), + folderTemplate: normalizeFolderTemplate(settings.folderTemplate), customFonts, fontFamily: normalizeFontFamily(settings.fontFamily, customFonts), }; diff --git a/frontend/src/types/api.ts b/frontend/src/types/api.ts index b0fd60d..3582a17 100644 --- a/frontend/src/types/api.ts +++ b/frontend/src/types/api.ts @@ -44,6 +44,7 @@ export interface TimelineEntry { verified?: boolean; original_filename?: string; author_username?: string; + author_name?: string; } export interface ExtractMetadata { new_entries: number; diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index 4dd9441..d605fd0 100644 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -187,6 +187,7 @@ export namespace main { content?: string; original_filename?: string; author_username?: string; + account_name?: string; static createFrom(source: any = {}) { return new MediaItemRequest(source); @@ -201,6 +202,7 @@ export namespace main { this.content = source["content"]; this.original_filename = source["original_filename"]; this.author_username = source["author_username"]; + this.account_name = source["account_name"]; } } export class DownloadMediaWithMetadataRequest { @@ -214,6 +216,9 @@ export namespace main { proxy?: string; filename_template?: string; folder_template?: string; + auto_convert_gifs?: boolean; + gif_quality?: string; + gif_resolution?: string; static createFrom(source: any = {}) { return new DownloadMediaWithMetadataRequest(source); @@ -231,6 +236,9 @@ export namespace main { this.proxy = source["proxy"]; this.filename_template = source["filename_template"]; this.folder_template = source["folder_template"]; + this.auto_convert_gifs = source["auto_convert_gifs"]; + this.gif_quality = source["gif_quality"]; + this.gif_resolution = source["gif_resolution"]; } convertValues(a: any, classs: any, asMap: boolean = false): any { diff --git a/wails.json b/wails.json index 51128fe..2b2494b 100644 --- a/wails.json +++ b/wails.json @@ -11,7 +11,7 @@ }, "info": { "productName": "Twitter/X Media Batch Downloader", - "productVersion": "4.7" + "productVersion": "4.8" }, "wailsjsdir": "./frontend", "assetdir": "./frontend/dist",