-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHashCloneFile.go
executable file
·45 lines (40 loc) · 1.21 KB
/
HashCloneFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package logic
import (
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
"errors"
"log"
"net/http"
"github.com/google/uuid"
)
func CloneFileByHash(fromHash string, toFolder uint, fileName string, userId uint) (status int, newFile *models.Link, err error) {
// check if requested folder exists (if set)
if toFolder > 0 {
res := inits.DB.First(&models.Folder{}, toFolder)
if res.Error != nil {
return http.StatusBadRequest, nil, errors.New("parent folder doesn't exist")
}
}
// check file hash with database
var existingFile models.File
if res := inits.DB.
Where(&models.File{
Hash: fromHash,
}).First(&existingFile); res.Error != nil {
return http.StatusNotFound, nil, errors.New("requested hash doesnt match any file")
}
// file is dublicate and can be linked
// link old uploaded file to new link
dbLink := models.Link{
UUID: uuid.NewString(),
ParentFolderID: toFolder,
UserID: userId,
FileID: existingFile.ID,
Name: fileName,
}
if res := inits.DB.Create(&dbLink); res.Error != nil {
log.Printf("Error saving link in database: %v", res.Error)
return http.StatusInternalServerError, nil, res.Error
}
return http.StatusOK, &dbLink, nil
}