skip write for already stored files

use-redis
Chakib Benziane 5 years ago
parent aa5f7f2f9e
commit 1284ae5a4a

1
.gitignore vendored

@ -1,2 +1,3 @@
bit4sat bit4sat
db-storage db-storage
file-storage

@ -1,6 +1,8 @@
package storage package storage
import ( import (
"errors"
"fmt"
"io" "io"
"log" "log"
"mime/multipart" "mime/multipart"
@ -24,7 +26,18 @@ func GetStoreDestination(filename string) string {
} }
func StoreFormFile(src multipart.File, filename string) error { func StoreFormFile(src multipart.File, filename string) error {
fd, err := os.Create(GetStoreDestination(filename)) filePath := GetStoreDestination(filename)
// If file exists just return
exists, err := CheckFileExists(filePath)
if err != nil {
return err
}
if exists {
log.Printf("file %s exists, skipping write", filename)
return nil
}
fd, err := os.Create(filePath)
defer fd.Close() defer fd.Close()
if err != nil { if err != nil {
return err return err
@ -36,6 +49,24 @@ func StoreFormFile(src multipart.File, filename string) error {
return err return err
} }
func CheckFileExists(file string) (bool, error) {
info, err := os.Stat(file)
if err == nil {
if info.IsDir() {
errMsg := fmt.Sprintf("'%s' is a directory", file)
return false, errors.New(errMsg)
}
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func init() { func init() {
path, set := os.LookupEnv(StoragePathEnv) path, set := os.LookupEnv(StoragePathEnv)
if !set { if !set {

Loading…
Cancel
Save