Handle file import digest

This commit is contained in:
nemunaire 2017-01-04 01:30:24 +01:00 committed by nemunaire
parent be05ace19d
commit a5c81196d4
4 changed files with 43 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import (
"bufio"
"crypto/sha512"
"encoding/base32"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@ -23,7 +24,7 @@ var RapidImport bool
type uploadedFile struct {
URI string
Digest []byte
Digest string
Path string
Parts []string
}
@ -94,7 +95,11 @@ func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error)
}
}
return exercice.ImportFile(pathname, fromURI)
if digest, err := hex.DecodeString(uf.Digest); err != nil {
return nil, err
} else {
return exercice.ImportFile(pathname, fromURI, digest)
}
}
func getCloudFile(pathname string, dest string) error {

View file

@ -28,7 +28,8 @@ new_file() (
THEME="$1"
EXERCICE="$2"
URI="$3"
ARGS="$4"
DIGEST="$4"
ARGS="$5"
FIRST=
PARTS=$(echo "$ARGS" | while read arg
@ -40,9 +41,15 @@ new_file() (
FIRST=1
done)
[ -n "${DIGEST}" ] && DIGEST=", \"digest\": \"${DIGEST}\""
cat <<EOF >&2
{"path": "${BASEFILE}${URI}"${DIGEST}, "parts": [${PARTS}]}
EOF
# curl -f -s -d "{\"URI\": \"${BASEFILE}${URI}\"}" "${BASEURL}/api/themes/$THEME/$EXERCICE/files" |
curl -f -s -d @- "${BASEURL}/api/themes/$THEME/exercices/$EXERCICE/files" <<EOF | grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+"
{"path": "${BASEFILE}${URI}", "parts": [${PARTS}]}
{"path": "${BASEFILE}${URI}"${DIGEST}, "parts": [${PARTS}]}
EOF
)
@ -192,6 +199,8 @@ do
# Files: splited
get_dir "${THM_BASEURI}${EXO_BASEURI}files/" | grep -v DIGESTS.txt | grep '[0-9][0-9]$' | sed -E 's/\.?([0-9][0-9])$//' | sort | uniq | while read f; do basename "$f"; done | while read FILE_URI
do
DIGEST=$(get_file "${THM_BASEURI}${EXO_BASEURI}files/DIGESTS.txt" | grep "${FILE_URI}\$" | awk '{ print $1; }')
PARTS=
for part in $(get_dir "${THM_BASEURI}${EXO_BASEURI}files/" | grep "${FILE_URI}" | sort)
do
@ -200,7 +209,7 @@ do
done
echo -e "\e[35mImport splited file ${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI} from\e[00m `echo ${PARTS} | tr '\n' ' '`"
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI}" "${PARTS}"`
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI}" "${DIGEST}" "${PARTS}"`
if [ -z "$FILE_ID" ]; then
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
else
@ -211,8 +220,10 @@ do
# Files: entire
get_dir "${THM_BASEURI}${EXO_BASEURI}files/" | grep -v DIGESTS.txt | grep -v '[0-9][0-9]$' | while read f; do basename "$f"; done | while read FILE_URI
do
DIGEST=$(get_file "${THM_BASEURI}${EXO_BASEURI}files/DIGESTS.txt" | grep "${FILE_URI}\$" | awk '{ print $1; }')
echo "Import file ${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI}"
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI}"`
FILE_ID=`new_file "${THEME_ID}" "${EXO_ID}" "${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI}" "${DIGEST}"`
if [ -z "$FILE_ID" ]; then
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
else

View file

@ -31,6 +31,7 @@ func main() {
flag.StringVar(&api.CloudUsername, "clouduser", "fic", "Username used to sync")
flag.StringVar(&api.CloudPassword, "cloudpass", "", "Password used to sync")
flag.BoolVar(&api.RapidImport, "rapidimport", false, "Don't try to reimport an existing file")
flag.BoolVar(&fic.OptionalDigest, "optionaldigest", false, "Is the digest required when importing files?")
flag.Parse()
log.SetPrefix("[admin] ")

View file

@ -3,6 +3,8 @@ package fic
import (
"bufio"
"crypto/sha1"
"encoding/hex"
"errors"
"io"
"os"
"path"
@ -10,6 +12,7 @@ import (
)
var FilesDir string
var OptionalDigest bool
type EFile struct {
Id int64 `json:"id"`
@ -66,8 +69,10 @@ func (e Exercice) GetFiles() ([]EFile, error) {
}
}
func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
if fi, err := os.Stat(filePath); err != nil {
func (e Exercice) ImportFile(filePath string, origin string, digest []byte) (EFile, error) {
if digest == nil && !OptionalDigest {
return EFile{}, errors.New("No digest given.")
} else if fi, err := os.Stat(filePath); err != nil {
return EFile{}, err
} else if fd, err := os.Open(filePath); err != nil {
return EFile{}, err
@ -80,8 +85,19 @@ func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
return EFile{}, err
}
var result []byte
return e.AddFile(strings.TrimPrefix(filePath, FilesDir), origin, path.Base(filePath), hash.Sum(result), fi.Size())
result := hash.Sum(nil)
if len(digest) != len(result) {
return EFile{}, errors.New("Digests doesn't match: calculated: " + hex.EncodeToString(result) + " vs. given: " + hex.EncodeToString(digest))
}
for k := range result {
if result[k] != digest[k] {
return EFile{}, errors.New("Digests doesn't match: calculated: " + hex.EncodeToString(result) + " vs. given: " + hex.EncodeToString(digest))
}
}
return e.AddFile(strings.TrimPrefix(filePath, FilesDir), origin, path.Base(filePath), result, fi.Size())
}
}