fix: uploading of main file

This commit is contained in:
Derrick Hammer 2023-05-04 09:11:31 -04:00
parent 5aca66d919
commit 7aea462ab7
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 16 additions and 5 deletions

View File

@ -40,14 +40,16 @@ func (f *FilesService) PostUpload() {
return
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(file)
buf, err := io.ReadAll(file)
if internalError(ctx, err) {
return
}
if internalErrorCustom(ctx, err, errors.New("failed to read file data")) {
return
}
hashBytes := blake3.Sum256(buf.Bytes())
hashBytes := blake3.Sum256(buf)
hashHex := hex.EncodeToString(hashBytes[:])
fileCid, err := cid.EncodeHashSimple(hashBytes, uint64(meta.Size))
@ -55,6 +57,11 @@ func (f *FilesService) PostUpload() {
return
}
_, err = file.Seek(0, io.SeekStart)
if internalError(ctx, err) {
return
}
var upload model.Upload
result := db.Get().Where("hash = ?", hashHex).First(&upload)
if (result.Error != nil && result.Error.Error() != "record not found") || result.RowsAffected > 0 {
@ -67,7 +74,7 @@ func (f *FilesService) PostUpload() {
return
}
tree, err := bao.ComputeBaoTree(file)
tree, err := bao.ComputeBaoTree(bytes.NewReader(buf))
if internalError(ctx, err) {
return
}
@ -83,7 +90,11 @@ func (f *FilesService) PostUpload() {
return
}
ret, err := client.R().SetBody(file).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
if internalError(ctx, err) {
return
}
ret, err := client.R().SetBody(buf).Put(fmt.Sprintf("/worker/objects/%s", hashHex))
if ret.StatusCode() != 200 {
err = errors.New(string(ret.Body()))
}