feat: begin working on the categorize
This commit is contained in:
@@ -14,7 +14,8 @@ import (
|
||||
)
|
||||
|
||||
func RegisterFileRoutes(file *gin.RouterGroup, env bootstrap.Environment, db *gorm.DB) {
|
||||
// /file/<file-name>
|
||||
// prefix: file
|
||||
// for example: /file/<file-name>
|
||||
file.GET("/:filename", func(c *gin.Context) {
|
||||
f := c.Param("filename")
|
||||
p := filepath.Join(env.ContentDirectory, f)
|
||||
|
||||
@@ -19,7 +19,7 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
|
||||
return File{}, fmt.Errorf("unsupported filetype")
|
||||
}
|
||||
|
||||
checksum, err := utility.HashReader(r)
|
||||
checksum, err := utility.GenerateHashFromReader(r)
|
||||
if err != nil {
|
||||
slog.Error("failed to calculate hash of file at given path", "error", err)
|
||||
return File{}, err
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package utility
|
||||
|
||||
type MediaType string
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
const (
|
||||
Video MediaType = "video"
|
||||
Presentation MediaType = "presentation"
|
||||
Internet MediaType = "internet"
|
||||
Unspecified MediaType = "unspecified"
|
||||
)
|
||||
|
||||
var (
|
||||
videoFormats = []string{".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a"}
|
||||
presentationFormats = []string{".pptx", ".ppt", ".key", ".odp"}
|
||||
)
|
||||
@@ -1,9 +1,25 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func GenerateHashFromReader(r io.Reader) (string, error) {
|
||||
h := sha512.New()
|
||||
if _, err := io.Copy(h, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// return the sha checksum in hex
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
// alternatively return in base64
|
||||
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func GenerateSafeName(category MediaType, ext string) string {
|
||||
return uuid.New().String() + "_" + string(category) + ext
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
)
|
||||
|
||||
func HashReader(r io.Reader) (string, error) {
|
||||
h := sha512.New()
|
||||
if _, err := io.Copy(h, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// return the sha checksum in hex
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
// alternatively return in base64
|
||||
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
@@ -6,15 +6,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MediaType string
|
||||
|
||||
const (
|
||||
Video MediaType = "video"
|
||||
Presentation MediaType = "presentation"
|
||||
Internet MediaType = "internet"
|
||||
Unspecified MediaType = "unspecified"
|
||||
)
|
||||
|
||||
func RemoveFile(p string) error {
|
||||
err := os.Remove(p)
|
||||
if err != nil {
|
||||
@@ -24,21 +15,20 @@ func RemoveFile(p string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
func CategorizeMediaType(ext string) (MediaType, bool) {
|
||||
// Lets categorize
|
||||
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
||||
return Video, true
|
||||
case ".pptx", ".ppt", ".key", ".odp":
|
||||
return Presentation, true
|
||||
default:
|
||||
slog.Debug("marking file as invalid due to its undefined extension")
|
||||
return "", false
|
||||
}
|
||||
/*
|
||||
switch ext {
|
||||
case slices.Contains(videoFormats, ext):
|
||||
return Video, true
|
||||
case slices.Contains(presentationFormats, ext):
|
||||
return Presentation, true
|
||||
default:
|
||||
slog.Debug("marking file as invalid due to its undefined extension")
|
||||
return "", false
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func ParseSlogLevel(s string) slog.Level {
|
||||
|
||||
Reference in New Issue
Block a user