mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-11-30 18:17:59 +01:00
v4.5
This commit is contained in:
1 parent
84b067ebd4
commit
e67d09a248
47 files changed
+3688
-958
No files matched your search
+344
-106
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user