[WIP] Merge splitted files

This commit is contained in:
nemunaire 2016-12-14 13:14:22 +01:00
parent 84f0229044
commit 0083c6ec17
2 changed files with 69 additions and 37 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/base32"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
@ -19,8 +20,15 @@ var CloudDAVBase string
var CloudUsername string
var CloudPassword string
type uploadedFile struct {
URI string
Digest []byte
Path string
Parts []string
}
func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
var uf map[string]string
var uf uploadedFile
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
}
@ -30,16 +38,39 @@ func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args map[string]
var fromURI string
var getFile func(string) (error)
if URI, ok := uf["URI"]; ok {
hash = sha512.Sum512([]byte(URI))
logStr = "Import file from Cloud: " + URI + " =>"
fromURI = URI
getFile = func(dest string) error { return getCloudFile(URI, dest); }
} else if path, ok := uf["path"]; ok {
hash = sha512.Sum512([]byte(path))
logStr = "Import file from local FS: " + path + " =>"
fromURI = path
getFile = func(dest string) error { return os.Symlink(path, dest); }
if uf.URI != "" {
hash = sha512.Sum512([]byte(uf.URI))
logStr = "Import file from Cloud: " + uf.URI + " =>"
fromURI = uf.URI
getFile = func(dest string) error { return getCloudFile(uf.URI, dest); }
} else if uf.Path != "" && len(uf.Parts) > 0 {
hash = sha512.Sum512([]byte(uf.Path))
logStr = fmt.Sprintf("Import file from local FS: %s =>", uf.Parts)
fromURI = uf.Path
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 fdfrm, err := os.Open(partname); err != nil {
return err
} else {
reader := bufio.NewReader(fdfrm)
reader.WriteTo(writer)
writer.Flush()
fdfrm.Close()
}
}
fdto.Close()
}
return nil
}
} else if uf.Path != "" {
hash = sha512.Sum512([]byte(uf.Path))
logStr = "Import file from local FS: " + uf.Path + " =>"
fromURI = uf.Path
getFile = func(dest string) error { return os.Symlink(uf.Path, dest); }
} else {
return nil, errors.New("URI or path not filled")
}