mirror of https://github.com/cloudreve/Cloudreve
feat(remote download): sanitize file names with special characters (#2648)
parent
195d68c535
commit
d19fc0e75c
2
assets
2
assets
|
@ -1 +1 @@
|
||||||
Subproject commit aa80ca5bb2609d75fc9706d12edd5c6e9a0c76b7
|
Subproject commit 3a6a22bb458783fcfd32f3b35f5fe6afc5414e25
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
@ -318,7 +319,7 @@ func (m *RemoteDownloadTask) slaveTransfer(ctx context.Context, dep dependency.D
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
dst := dstUri.JoinRaw(f.Name)
|
dst := dstUri.JoinRaw(sanitizeFileName(f.Name))
|
||||||
src := path.Join(m.state.Status.SavePath, f.Name)
|
src := path.Join(m.state.Status.SavePath, f.Name)
|
||||||
payload.Files = append(payload.Files, SlaveUploadEntity{
|
payload.Files = append(payload.Files, SlaveUploadEntity{
|
||||||
Src: src,
|
Src: src,
|
||||||
|
@ -437,9 +438,10 @@ func (m *RemoteDownloadTask) masterTransfer(ctx context.Context, dep dependency.
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
dst := dstUri.JoinRaw(file.Name)
|
sanitizedName := sanitizeFileName(file.Name)
|
||||||
|
dst := dstUri.JoinRaw(sanitizedName)
|
||||||
src := filepath.FromSlash(path.Join(m.state.Status.SavePath, file.Name))
|
src := filepath.FromSlash(path.Join(m.state.Status.SavePath, file.Name))
|
||||||
m.l.Info("Uploading file %s to %s...", src, file.Name, dst)
|
m.l.Info("Uploading file %s to %s...", src, sanitizedName, dst)
|
||||||
|
|
||||||
progressKey := fmt.Sprintf("%s%d", ProgressTypeUploadSinglePrefix, workerId)
|
progressKey := fmt.Sprintf("%s%d", ProgressTypeUploadSinglePrefix, workerId)
|
||||||
m.Lock()
|
m.Lock()
|
||||||
|
@ -538,7 +540,7 @@ func (m *RemoteDownloadTask) validateFiles(ctx context.Context, dep dependency.D
|
||||||
|
|
||||||
validateArgs := lo.Map(selectedFiles, func(f downloader.TaskFile, _ int) fs.PreValidateFile {
|
validateArgs := lo.Map(selectedFiles, func(f downloader.TaskFile, _ int) fs.PreValidateFile {
|
||||||
return fs.PreValidateFile{
|
return fs.PreValidateFile{
|
||||||
Name: f.Name,
|
Name: sanitizeFileName(f.Name),
|
||||||
Size: f.Size,
|
Size: f.Size,
|
||||||
OmitName: f.Name == "",
|
OmitName: f.Name == "",
|
||||||
}
|
}
|
||||||
|
@ -637,3 +639,8 @@ func (m *RemoteDownloadTask) Progress(ctx context.Context) queue.Progresses {
|
||||||
}
|
}
|
||||||
return m.progress
|
return m.progress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sanitizeFileName(name string) string {
|
||||||
|
r := strings.NewReplacer("\\", "_", ":", "_", "*", "_", "?", "_", "\"", "_", "<", "_", ">", "_", "|", "_")
|
||||||
|
return r.Replace(name)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue