summaryrefslogtreecommitdiff
path: root/kcore/file.go
blob: f7f5e37d673a7f81fcde3af076f92cd3c11ed2bb (plain) (blame)
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

package kcore

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
}