diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 309a0d71..2fd06c57 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -124,6 +124,7 @@ type uploadedHint struct { Title string Content string Cost int64 + URI string Path string } @@ -135,8 +136,8 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) if len(uh.Content) != 0 { return exercice.AddHint(uh.Title, uh.Content, uh.Cost) - } else if len(uh.Path) != 0 { - return importFile(uploadedFile{Path: uh.Path}, + } else if len(uh.Path) != 0 || len(uh.URI) != 0 { + return importFile(uploadedFile{Path: uh.Path, URI: uh.URI}, func(filePath string, origin string, digest []byte) (interface{}, error) { return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost) }) diff --git a/admin/api/file.go b/admin/api/file.go index f9614a26..cc30398e 100644 --- a/admin/api/file.go +++ b/admin/api/file.go @@ -34,7 +34,25 @@ func importFile(uf uploadedFile, next func(string, string, []byte) (interface{}, var fromURI string var getFile func(string) (error) - if uf.URI != "" { + if uf.URI != "" && len(uf.Parts) > 0 { + hash = sha512.Sum512([]byte(uf.URI)) + logStr = fmt.Sprintf("Import file from Cloud: %s =>", uf.Parts) + fromURI = uf.URI + getFile = func(dest string) error { + if fdto, err := os.Create(dest); err != nil { + return err + } else { + writer := bufio.NewWriter(fdto) + for _, partname := range uf.Parts { + if err := getCloudPart(partname, writer); err != nil { + return err + } + } + fdto.Close() + } + return nil + } + } else if uf.URI != "" { hash = sha512.Sum512([]byte(uf.URI)) logStr = "Import file from Cloud: " + uf.URI + " =>" fromURI = uf.URI @@ -74,6 +92,7 @@ func importFile(uf uploadedFile, next func(string, string, []byte) (interface{}, pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(fromURI)) // Remove the file if it exists + // TODO: check if this is symlink => remove to avoid File not found error after, because the file is writen at the adresse pointed. if _, err := os.Stat(pathname); !os.IsNotExist(err) && !RapidImport { if err := os.Remove(pathname); err != nil { return nil, err @@ -97,6 +116,20 @@ func importFile(uf uploadedFile, next func(string, string, []byte) (interface{}, } func getCloudFile(pathname string, dest string) error { + if fd, err := os.Create(dest); err != nil { + return err + } else { + defer fd.Close() + + writer := bufio.NewWriter(fd) + if err := getCloudPart(pathname, writer); err != nil { + return err + } + } + return nil +} + +func getCloudPart(pathname string, writer *bufio.Writer) error { client := http.Client{} if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil { return err @@ -107,19 +140,12 @@ func getCloudFile(pathname string, dest string) error { } else { defer resp.Body.Close() - if fd, err := os.Create(dest); err != nil { - return err + if resp.StatusCode != http.StatusOK { + return errors.New(resp.Status) } else { - defer fd.Close() - - if resp.StatusCode != http.StatusOK { - return errors.New(resp.Status) - } else { - writer := bufio.NewWriter(fd) - reader := bufio.NewReader(resp.Body) - reader.WriteTo(writer) - writer.Flush() - } + reader := bufio.NewReader(resp.Body) + reader.WriteTo(writer) + writer.Flush() } } }