Handle file import digest
This commit is contained in:
parent
be05ace19d
commit
a5c81196d4
4 changed files with 43 additions and 10 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -23,7 +24,7 @@ var RapidImport bool
|
||||||
|
|
||||||
type uploadedFile struct {
|
type uploadedFile struct {
|
||||||
URI string
|
URI string
|
||||||
Digest []byte
|
Digest string
|
||||||
Path string
|
Path string
|
||||||
Parts []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 {
|
func getCloudFile(pathname string, dest string) error {
|
||||||
|
|
|
@ -28,7 +28,8 @@ new_file() (
|
||||||
THEME="$1"
|
THEME="$1"
|
||||||
EXERCICE="$2"
|
EXERCICE="$2"
|
||||||
URI="$3"
|
URI="$3"
|
||||||
ARGS="$4"
|
DIGEST="$4"
|
||||||
|
ARGS="$5"
|
||||||
|
|
||||||
FIRST=
|
FIRST=
|
||||||
PARTS=$(echo "$ARGS" | while read arg
|
PARTS=$(echo "$ARGS" | while read arg
|
||||||
|
@ -40,9 +41,15 @@ new_file() (
|
||||||
FIRST=1
|
FIRST=1
|
||||||
done)
|
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 "{\"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]+"
|
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
|
EOF
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -192,6 +199,8 @@ do
|
||||||
# Files: splited
|
# 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
|
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
|
do
|
||||||
|
DIGEST=$(get_file "${THM_BASEURI}${EXO_BASEURI}files/DIGESTS.txt" | grep "${FILE_URI}\$" | awk '{ print $1; }')
|
||||||
|
|
||||||
PARTS=
|
PARTS=
|
||||||
for part in $(get_dir "${THM_BASEURI}${EXO_BASEURI}files/" | grep "${FILE_URI}" | sort)
|
for part in $(get_dir "${THM_BASEURI}${EXO_BASEURI}files/" | grep "${FILE_URI}" | sort)
|
||||||
do
|
do
|
||||||
|
@ -200,7 +209,7 @@ do
|
||||||
done
|
done
|
||||||
echo -e "\e[35mImport splited file ${THM_BASEURI}${EXO_BASEURI}files/${FILE_URI} from\e[00m `echo ${PARTS} | tr '\n' ' '`"
|
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
|
if [ -z "$FILE_ID" ]; then
|
||||||
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
||||||
else
|
else
|
||||||
|
@ -211,8 +220,10 @@ do
|
||||||
# Files: entire
|
# 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
|
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
|
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}"
|
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
|
if [ -z "$FILE_ID" ]; then
|
||||||
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
echo -e "\e[31;01m!!! An error occured during file import! Please check path.\e[00m"
|
||||||
else
|
else
|
||||||
|
|
|
@ -31,6 +31,7 @@ func main() {
|
||||||
flag.StringVar(&api.CloudUsername, "clouduser", "fic", "Username used to sync")
|
flag.StringVar(&api.CloudUsername, "clouduser", "fic", "Username used to sync")
|
||||||
flag.StringVar(&api.CloudPassword, "cloudpass", "", "Password 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(&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()
|
flag.Parse()
|
||||||
|
|
||||||
log.SetPrefix("[admin] ")
|
log.SetPrefix("[admin] ")
|
||||||
|
|
|
@ -3,6 +3,8 @@ package fic
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -10,6 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var FilesDir string
|
var FilesDir string
|
||||||
|
var OptionalDigest bool
|
||||||
|
|
||||||
type EFile struct {
|
type EFile struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
|
@ -66,8 +69,10 @@ func (e Exercice) GetFiles() ([]EFile, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
|
func (e Exercice) ImportFile(filePath string, origin string, digest []byte) (EFile, error) {
|
||||||
if fi, err := os.Stat(filePath); err != nil {
|
if digest == nil && !OptionalDigest {
|
||||||
|
return EFile{}, errors.New("No digest given.")
|
||||||
|
} else if fi, err := os.Stat(filePath); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
} else if fd, err := os.Open(filePath); err != nil {
|
} else if fd, err := os.Open(filePath); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
|
@ -80,8 +85,19 @@ func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []byte
|
result := hash.Sum(nil)
|
||||||
return e.AddFile(strings.TrimPrefix(filePath, FilesDir), origin, path.Base(filePath), hash.Sum(result), fi.Size())
|
|
||||||
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue