admin: allow import of remote hint and partials remote parts
This commit is contained in:
parent
1af5a92f80
commit
23dc467631
|
@ -124,6 +124,7 @@ type uploadedHint struct {
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
Cost int64
|
Cost int64
|
||||||
|
URI string
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,8 +136,8 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error)
|
||||||
|
|
||||||
if len(uh.Content) != 0 {
|
if len(uh.Content) != 0 {
|
||||||
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
||||||
} else if len(uh.Path) != 0 {
|
} else if len(uh.Path) != 0 || len(uh.URI) != 0 {
|
||||||
return importFile(uploadedFile{Path: uh.Path},
|
return importFile(uploadedFile{Path: uh.Path, URI: uh.URI},
|
||||||
func(filePath string, origin string, digest []byte) (interface{}, error) {
|
func(filePath string, origin string, digest []byte) (interface{}, error) {
|
||||||
return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost)
|
return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost)
|
||||||
})
|
})
|
||||||
|
|
|
@ -34,7 +34,25 @@ func importFile(uf uploadedFile, next func(string, string, []byte) (interface{},
|
||||||
var fromURI string
|
var fromURI string
|
||||||
var getFile func(string) (error)
|
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))
|
hash = sha512.Sum512([]byte(uf.URI))
|
||||||
logStr = "Import file from Cloud: " + uf.URI + " =>"
|
logStr = "Import file from Cloud: " + uf.URI + " =>"
|
||||||
fromURI = 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))
|
pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(fromURI))
|
||||||
|
|
||||||
// Remove the file if it exists
|
// 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.Stat(pathname); !os.IsNotExist(err) && !RapidImport {
|
||||||
if err := os.Remove(pathname); err != nil {
|
if err := os.Remove(pathname); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -97,6 +116,20 @@ func importFile(uf uploadedFile, next func(string, string, []byte) (interface{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCloudFile(pathname string, dest string) error {
|
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{}
|
client := http.Client{}
|
||||||
if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil {
|
if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -107,19 +140,12 @@ func getCloudFile(pathname string, dest string) error {
|
||||||
} else {
|
} else {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if fd, err := os.Create(dest); err != nil {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return err
|
return errors.New(resp.Status)
|
||||||
} else {
|
} else {
|
||||||
defer fd.Close()
|
reader := bufio.NewReader(resp.Body)
|
||||||
|
reader.WriteTo(writer)
|
||||||
if resp.StatusCode != http.StatusOK {
|
writer.Flush()
|
||||||
return errors.New(resp.Status)
|
|
||||||
} else {
|
|
||||||
writer := bufio.NewWriter(fd)
|
|
||||||
reader := bufio.NewReader(resp.Body)
|
|
||||||
reader.WriteTo(writer)
|
|
||||||
writer.Flush()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user