feat: add --backup-converted flag to keep .orig backups
This commit is contained in:
@@ -236,6 +236,9 @@ func ApplyFlags(c *http.Config, f *Flags, cfg *config.Config, cookieJar *cookie.
|
|||||||
if *f.AdjustExt {
|
if *f.AdjustExt {
|
||||||
c.Recursive.AdjustExt = true
|
c.Recursive.AdjustExt = true
|
||||||
}
|
}
|
||||||
|
if *f.BackupConverted {
|
||||||
|
c.Recursive.BackupConverted = true
|
||||||
|
}
|
||||||
|
|
||||||
// ── Cookies from CLI flags ──
|
// ── Cookies from CLI flags ──
|
||||||
if len(f.Cookies) > 0 {
|
if len(f.Cookies) > 0 {
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ type MirrorConfig struct {
|
|||||||
ConvertLinks bool // Convert links for local viewing
|
ConvertLinks bool // Convert links for local viewing
|
||||||
DownloadAssets bool // Download CSS, JS, images
|
DownloadAssets bool // Download CSS, JS, images
|
||||||
PruneEmpty bool // Remove empty directories after download
|
PruneEmpty bool // Remove empty directories after download
|
||||||
|
BackupConverted bool // Keep .orig backup before link conversion
|
||||||
RestrictFiles bool // Only download files from original host
|
RestrictFiles bool // Only download files from original host
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
RateLimiter *transport.TokenBucket
|
RateLimiter *transport.TokenBucket
|
||||||
@@ -642,6 +643,16 @@ func (m *Mirror) convertLinksInFile(filePath string) {
|
|||||||
converted := m.rewriter.RewriteLinks(doc, filePath)
|
converted := m.rewriter.RewriteLinks(doc, filePath)
|
||||||
|
|
||||||
if converted > 0 {
|
if converted > 0 {
|
||||||
|
// Backup original file before link conversion.
|
||||||
|
if m.config.BackupConverted {
|
||||||
|
backupPath := filePath + ".orig"
|
||||||
|
if err := os.WriteFile(backupPath, data, 0644); err != nil {
|
||||||
|
if m.config.Verbose {
|
||||||
|
fmt.Fprintf(os.Stderr, "[error] failed to backup %s: %v\n", filePath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write modified HTML
|
// Write modified HTML
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := html.Render(&buf, doc); err != nil {
|
if err := html.Render(&buf, doc); err != nil {
|
||||||
|
|||||||
@@ -403,6 +403,7 @@ func (c *Client) downloadMirror(ctx context.Context, req *core.DownloadRequest)
|
|||||||
ConvertLinks: c.config.Recursive.ConvertLinks,
|
ConvertLinks: c.config.Recursive.ConvertLinks,
|
||||||
DownloadAssets: c.config.Recursive.MirrorAssets,
|
DownloadAssets: c.config.Recursive.MirrorAssets,
|
||||||
PruneEmpty: true,
|
PruneEmpty: true,
|
||||||
|
BackupConverted: c.config.Recursive.BackupConverted,
|
||||||
RestrictFiles: true,
|
RestrictFiles: true,
|
||||||
Timeout: c.config.Transport.Timeout,
|
Timeout: c.config.Transport.Timeout,
|
||||||
RespectRobots: c.config.Recursive.RespectRobots,
|
RespectRobots: c.config.Recursive.RespectRobots,
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ type RecursiveConfig struct {
|
|||||||
Mirror bool
|
Mirror bool
|
||||||
MirrorAssets bool
|
MirrorAssets bool
|
||||||
ConvertLinks bool
|
ConvertLinks bool
|
||||||
|
BackupConverted bool
|
||||||
RespectRobots bool
|
RespectRobots bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user