Skip to content

Commit

Permalink
improoved metadata reading of uploaded file by pre-encode it
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 6, 2024
1 parent f90f1a2 commit 460c28a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion controllers/CreateFileController.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CreateFile(c *fiber.Ctx) error {
}

// business logic
status, dbLink, cloned, err := logic.CreateFile(filePath, fileValidation.ParentFolderID, file.Filename, fileId, file.Size, c.Locals("UserID").(uint))
status, dbLink, cloned, err := logic.CreateFile(&filePath, fileValidation.ParentFolderID, file.Filename, fileId, file.Size, c.Locals("UserID").(uint))
if err != nil {
return c.Status(status).SendString(err.Error())
}
Expand Down
43 changes: 38 additions & 5 deletions logic/CreateFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"fmt"
"log"
"math"
"os"
"os/exec"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -20,7 +23,7 @@ import (
"gorm.io/gorm"
)

func CreateFile(fromFile string, toFolder uint, fileName string, fileId string, fileSize int64, userId uint) (status int, newFile *models.Link, cloned bool, err error) {
func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, fileSize int64, userId uint) (status int, newFile *models.Link, cloned bool, err error) {
//check if requested folder exists (if set)
if toFolder > 0 {
res := inits.DB.First(&models.Folder{}, toFolder)
Expand All @@ -30,7 +33,7 @@ func CreateFile(fromFile string, toFolder uint, fileName string, fileId string,
}

// obtain hash from file
FileHash, err := helpers.HashFile(fromFile)
FileHash, err := helpers.HashFile(*fromFile)
if err != nil {
log.Printf("Failed to create hash from file: %v", err)
return fiber.StatusInternalServerError, nil, false, fiber.ErrInternalServerError
Expand All @@ -42,12 +45,42 @@ func CreateFile(fromFile string, toFolder uint, fileName string, fileId string,
return status, newLink, true, err
}

// run file through ffmpeg so the metadata is more accurate
nameSplits := strings.Split(fileName, ".")
fileExt := nameSplits[len(nameSplits)-1]
oldOutPath := *fromFile
newOutPath := fmt.Sprintf("%s.%s", *fromFile, fileExt)
fromFile = &newOutPath

// check if file extension is supported
if !slices.Contains(config.EXTENSIONS, strings.ToLower(fileExt)) {
return fiber.StatusBadRequest, nil, false, errors.New("Video extension is not supported")
}

ffmpegCommand := "ffmpeg " +
fmt.Sprintf(`-i "%s" `, oldOutPath) + // input file
"-map 0 -c copy " +
fmt.Sprintf(`"%s"`, newOutPath) // output file

cmd := exec.Command(
"bash",
"-c",
ffmpegCommand)
if err := cmd.Run(); err != nil {
log.Printf("Error happend while copy encoding: %v\n", err.Error())
log.Println(ffmpegCommand)
return fiber.StatusInternalServerError, nil, false, fiber.ErrInternalServerError
}
if err := os.Remove(oldOutPath); err != nil {
log.Printf("Failed to delete oldInputEncoding File %s: %v\n", oldOutPath, err.Error())
}

// ffprobe context
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()

// probe file
data, err := ffprobe.ProbeURL(ctx, fromFile)
data, err := ffprobe.ProbeURL(ctx, *fromFile)
if err != nil {
log.Printf("Error getting data using ffprobe: %v", err)
return fiber.StatusInternalServerError, nil, false, fiber.ErrInternalServerError
Expand Down Expand Up @@ -137,7 +170,7 @@ func CreateFile(fromFile string, toFolder uint, fileName string, fileId string,
go func() {
if _, err := CreateThumbnail(
4,
fromFile,
*fromFile,
1080,
thumbnailFileName,
fmt.Sprintf("%s/%s", config.ENV.FolderVideoQualitysPriv, fileId),
Expand All @@ -156,7 +189,7 @@ func CreateFile(fromFile string, toFolder uint, fileName string, fileId string,
UUID: fileId,
Hash: FileHash,
Thumbnail: thumbnailFileName,
Path: fromFile,
Path: *fromFile,
Folder: fmt.Sprintf("%s/%s", config.ENV.FolderVideoQualitysPriv, fileId),
UserID: userId,
Height: int64(videoHeight),
Expand Down
4 changes: 2 additions & 2 deletions logic/CreateUploadFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m
return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError
}

// copy uploaded chuncks into final file
var written int64

for _, uploadChunck := range uploadChuncks {
openedChunck, err := os.Open(uploadChunck.Path)
if err != nil {
Expand Down Expand Up @@ -109,7 +109,7 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m
log.Printf("Failed to copy final file to destination: %v", err)
return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError
}
status, dbLink, cloned, err := CreateFile(filePath, uploadSession.ParentFolderID, uploadSession.Name, fileId, uploadSession.Size, userId)
status, dbLink, cloned, err := CreateFile(&filePath, uploadSession.ParentFolderID, uploadSession.Name, fileId, uploadSession.Size, userId)
if err != nil {
os.Remove(filePath)
return status, nil, err
Expand Down

0 comments on commit 460c28a

Please sign in to comment.