sync: Export DownloadExerciceFile function

This commit is contained in:
nemunaire 2023-11-25 18:38:12 +01:00
parent f3cdf1afca
commit c3353572e6
3 changed files with 11 additions and 11 deletions

View File

@ -194,9 +194,9 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExc
if pf, exists := paramsFiles[fname]; exists && pf.URL != "" { if pf, exists := paramsFiles[fname]; exists && pf.URL != "" {
if li, ok := i.(LocalImporter); ok { if li, ok := i.(LocalImporter); ok {
errs = multierr.Append(errs, downloadExerciceFile(paramsFiles[fname], li.GetLocalPath(dest), exercice, false)) errs = multierr.Append(errs, DownloadExerciceFile(paramsFiles[fname], li.GetLocalPath(dest), exercice, false))
} else { } else {
errs = multierr.Append(errs, downloadExerciceFile(paramsFiles[fname], dest, exercice, false)) errs = multierr.Append(errs, DownloadExerciceFile(paramsFiles[fname], dest, exercice, false))
} }
} }
@ -253,7 +253,7 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExc
} }
} }
file := exercice.NewDummyFile(path.Join(exercice.Path, "files", fname), getDestinationFilePath(path.Join(exercice.Path, "files", fname), nil), (*hash512).Sum(nil), digest_shown, disclaimer, size) file := exercice.NewDummyFile(path.Join(exercice.Path, "files", fname), GetDestinationFilePath(path.Join(exercice.Path, "files", fname), nil), (*hash512).Sum(nil), digest_shown, disclaimer, size)
// Call checks hooks // Call checks hooks
for _, h := range hooks.fileHooks { for _, h := range hooks.fileHooks {
@ -269,8 +269,8 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExc
return return
} }
// downloadExerciceFile is responsible to fetch remote files. // DownloadExerciceFile is responsible to fetch remote files.
func downloadExerciceFile(pf ExerciceFile, dest string, exercice *fic.Exercice, force bool) (errs error) { func DownloadExerciceFile(pf ExerciceFile, dest string, exercice *fic.Exercice, force bool) (errs error) {
if st, err := os.Stat(dest); !force && !os.IsNotExist(err) { if st, err := os.Stat(dest); !force && !os.IsNotExist(err) {
resp, err := http.Head(pf.URL) resp, err := http.Head(pf.URL)
if err == nil && resp.ContentLength == st.Size() { if err == nil && resp.ContentLength == st.Size() {
@ -364,7 +364,7 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce
var f interface{} var f interface{}
if pf, exists := paramsFiles[fname]; exists && pf.URL != "" { if pf, exists := paramsFiles[fname]; exists && pf.URL != "" {
dest := getDestinationFilePath(pf.URL, &pf.Filename) dest := GetDestinationFilePath(pf.URL, &pf.Filename)
if _, err := os.Stat(dest); !os.IsNotExist(err) { if _, err := os.Stat(dest); !os.IsNotExist(err) {
if d, err := actionAfterImport(dest, pf.URL); err == nil { if d, err := actionAfterImport(dest, pf.URL); err == nil {
@ -373,7 +373,7 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce
} }
if f == nil { if f == nil {
errs = multierr.Append(errs, downloadExerciceFile(paramsFiles[fname], dest, exercice, false)) errs = multierr.Append(errs, DownloadExerciceFile(paramsFiles[fname], dest, exercice, false))
f, err = actionAfterImport(dest, pf.URL) f, err = actionAfterImport(dest, pf.URL)
} }

View File

@ -180,10 +180,10 @@ func GetFileContent(i Importer, URI string) (string, error) {
} }
} }
// getDestinationFilePath generates the destination path, from the URI. // GetDestinationFilePath generates the destination path, from the URI.
// This function permits to obfusce to player the original URI. // This function permits to obfusce to player the original URI.
// Theoricaly, changing the import method doesn't change destination URI. // Theoricaly, changing the import method doesn't change destination URI.
func getDestinationFilePath(URI string, filename *string) string { func GetDestinationFilePath(URI string, filename *string) string {
if filename == nil { if filename == nil {
tmp := path.Base(URI) tmp := path.Base(URI)
filename = &tmp filename = &tmp
@ -218,7 +218,7 @@ func importFile(i Importer, URI string, dest string) error {
// ImportFile imports the file at the given URI, using helpers of the given Importer. // ImportFile imports the file at the given URI, using helpers of the given Importer.
// After import, next is called with relative path where the file has been saved and the original URI. // After import, next is called with relative path where the file has been saved and the original URI.
func ImportFile(i Importer, URI string, next func(string, string) (interface{}, error)) (interface{}, error) { func ImportFile(i Importer, URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
dest := getDestinationFilePath(URI, nil) dest := GetDestinationFilePath(URI, nil)
// If the present file is still valide, don't erase it // If the present file is still valide, don't erase it
if _, err := os.Stat(dest); !os.IsNotExist(err) { if _, err := os.Stat(dest); !os.IsNotExist(err) {

View File

@ -62,7 +62,7 @@ func (i LocalImporter) GetLocalPath(p ...string) string {
func (i LocalImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) { func (i LocalImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
if i.Symlink { if i.Symlink {
dest := getDestinationFilePath(URI, nil) dest := GetDestinationFilePath(URI, nil)
if err := os.MkdirAll(path.Dir(dest), 0751); err != nil { if err := os.MkdirAll(path.Dir(dest), 0751); err != nil {
return nil, err return nil, err