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) {
|
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) {
|
file.GET("/:filename", func(c *gin.Context) {
|
||||||
f := c.Param("filename")
|
f := c.Param("filename")
|
||||||
p := filepath.Join(env.ContentDirectory, f)
|
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")
|
return File{}, fmt.Errorf("unsupported filetype")
|
||||||
}
|
}
|
||||||
|
|
||||||
checksum, err := utility.HashReader(r)
|
checksum, err := utility.GenerateHashFromReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to calculate hash of file at given path", "error", err)
|
slog.Error("failed to calculate hash of file at given path", "error", err)
|
||||||
return File{}, 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
|
package utility
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha512"
|
||||||
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"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 {
|
func GenerateSafeName(category MediaType, ext string) string {
|
||||||
return uuid.New().String() + "_" + string(category) + ext
|
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"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MediaType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Video MediaType = "video"
|
|
||||||
Presentation MediaType = "presentation"
|
|
||||||
Internet MediaType = "internet"
|
|
||||||
Unspecified MediaType = "unspecified"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RemoveFile(p string) error {
|
func RemoveFile(p string) error {
|
||||||
err := os.Remove(p)
|
err := os.Remove(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -24,21 +15,20 @@ func RemoveFile(p string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0: unspecified
|
|
||||||
// 1: video
|
|
||||||
// 2: presentation
|
|
||||||
// 3: internet URL
|
|
||||||
func CategorizeMediaType(ext string) (MediaType, bool) {
|
func CategorizeMediaType(ext string) (MediaType, bool) {
|
||||||
|
// Lets categorize
|
||||||
|
|
||||||
|
/*
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
case slices.Contains(videoFormats, ext):
|
||||||
return Video, true
|
return Video, true
|
||||||
case ".pptx", ".ppt", ".key", ".odp":
|
case slices.Contains(presentationFormats, ext):
|
||||||
return Presentation, true
|
return Presentation, true
|
||||||
default:
|
default:
|
||||||
slog.Debug("marking file as invalid due to its undefined extension")
|
slog.Debug("marking file as invalid due to its undefined extension")
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseSlogLevel(s string) slog.Level {
|
func ParseSlogLevel(s string) slog.Level {
|
||||||
|
|||||||
Reference in New Issue
Block a user