You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bit4sat/storage/storage.go

53 lines
774 B
Go

package storage
import (
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
"git.sp4ke.com/sp4ke/bit4sat-server.git/utils"
)
const (
StoragePath = "file-storage"
StoragePathEnv = "BIT4SAT_STORAGE_PATH"
)
var (
RuntimeStoragePath string
)
func GetStoreDestination(filename string) string {
return filepath.Join(RuntimeStoragePath, filename)
}
func StoreFormFile(src multipart.File, filename string) error {
fd, err := os.Create(GetStoreDestination(filename))
defer fd.Close()
if err != nil {
return err
}
src.Seek(0, 0)
_, err = io.Copy(fd, src)
return err
}
func init() {
path, set := os.LookupEnv(StoragePathEnv)
if !set {
path = StoragePath
}
RuntimeStoragePath = path
err := utils.Mkdir(path)
if err != nil {
log.Fatal(err)
}
}