aboutsummaryrefslogtreecommitdiff
path: root/core/file.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-10-24 07:20:10 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-10-24 07:20:29 +0200
commit18476c6cba2d5d2bb3ecf3ad136dacbf41d0360e (patch)
treea7a958ff7a4bf14ed7f7ee3ffd9c84b7592facbd /core/file.go
feat: Extract from other repo
Diffstat (limited to 'core/file.go')
-rw-r--r--core/file.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/core/file.go b/core/file.go
new file mode 100644
index 0000000..083d8f7
--- /dev/null
+++ b/core/file.go
@@ -0,0 +1,38 @@
+package core
+
+import (
+ "fmt"
+ "io"
+ "mime/multipart"
+ "os"
+)
+
+type File (string)
+
+func NewFile(ext string) File {
+ return File(NewID().String() + ext)
+}
+
+func (file File) Path() string {
+ return fmt.Sprintf("media/files/%s", file)
+}
+
+func (file *File) Save(raw multipart.File) error {
+ dest, err := os.Create(file.Path())
+ if err != nil {
+ return Wrap(err, "error creating file destination")
+ }
+ _, err = io.Copy(dest, raw)
+ if err != nil {
+ return Wrap(err, "error copying to file destination")
+ }
+ return nil
+}
+
+func (file *File) Delete() error {
+ err := os.Remove(file.Path())
+ if err != nil {
+ return Wrap(err, "error deleting file")
+ }
+ return nil
+}