refactor: add option to SaveUpload so we have the option not to change an upload record if we are uploading or importing a file

This commit is contained in:
Derrick Hammer 2024-03-21 15:45:52 -04:00
parent 8bf4887dae
commit a8f62fd666
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 11 additions and 7 deletions

View File

@ -484,7 +484,7 @@ func (s *S5API) smallFileUpload(jc jape.Context) {
newUpload.UserID = user
newUpload.UploaderIP = jc.Request.RemoteAddr
err2 = s.metadata.SaveUpload(jc.Request.Context(), *newUpload)
err2 = s.metadata.SaveUpload(jc.Request.Context(), *newUpload, true)
if err2 != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileUploadFailed, err2))
@ -1359,7 +1359,7 @@ func (s *S5API) processMultipartFiles(r *http.Request) (map[string]*metadata.Upl
upload.UserID = user
upload.UploaderIP = r.RemoteAddr
err = s.metadata.SaveUpload(r.Context(), *upload)
err = s.metadata.SaveUpload(r.Context(), *upload, true)
if err != nil {
return nil, NewS5Error(ErrKeyStorageOperationFailed, err)
}
@ -1430,7 +1430,7 @@ func (s *S5API) uploadAppMetadata(appData *s5libmetadata.WebAppMetadata, r *http
upload.UserID = userId
upload.UploaderIP = r.RemoteAddr
err = s.metadata.SaveUpload(r.Context(), *upload)
err = s.metadata.SaveUpload(r.Context(), *upload, true)
if err != nil {
return "", NewS5Error(ErrKeyStorageOperationFailed, err)
}
@ -2004,7 +2004,7 @@ func (s *S5API) pinImportCronJob(cid string, url string, proofUrl string, userId
saveAndPin := func(upload *metadata.UploadMetadata) error {
upload.UserID = userId
if err := s.metadata.SaveUpload(ctx, *upload); err != nil {
if err := s.metadata.SaveUpload(ctx, *upload, true); err != nil {
return err
}

View File

@ -50,7 +50,7 @@ var Module = fx.Module("metadata",
)
type MetadataService interface {
SaveUpload(ctx context.Context, metadata UploadMetadata) error
SaveUpload(ctx context.Context, metadata UploadMetadata, skipExisting bool) error
GetUpload(ctx context.Context, objectHash []byte) (UploadMetadata, error)
DeleteUpload(ctx context.Context, objectHash []byte) error
}
@ -70,7 +70,7 @@ func NewMetadataService(params MetadataServiceParams) *MetadataServiceDefault {
}
}
func (m *MetadataServiceDefault) SaveUpload(ctx context.Context, metadata UploadMetadata) error {
func (m *MetadataServiceDefault) SaveUpload(ctx context.Context, metadata UploadMetadata, skipExisting bool) error {
var upload models.Upload
upload.Hash = metadata.Hash
@ -84,6 +84,10 @@ func (m *MetadataServiceDefault) SaveUpload(ctx context.Context, metadata Upload
return ret.Error
}
if skipExisting {
return nil
}
changed := false
if upload.UserID != metadata.UserID {

View File

@ -435,7 +435,7 @@ func (t *TusHandler) uploadTask(hash []byte) error {
uploadMeta.UserID = upload.UploaderID
uploadMeta.UploaderIP = upload.UploaderIP
err = t.metadata.SaveUpload(ctx, *uploadMeta)
err = t.metadata.SaveUpload(ctx, *uploadMeta, true)
if err != nil {
t.logger.Error("Could not create upload", zap.Error(err))
return err