This commit is contained in:
minoplhy 2024-04-10 00:07:53 +07:00
commit 52007d940e
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
7 changed files with 358 additions and 0 deletions

4
.env Normal file
View File

@ -0,0 +1,4 @@
# something like this ->
# https://domain.tld
CHIBISAFE_BASEPATH=
HOST=

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
uploads

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/minoplhy/chibisafe_netstorage_middleman
go 1.22.2
require github.com/joho/godotenv v1.5.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

116
main.go Normal file

File diff suppressed because it is too large Load Diff

170
src/handler/chibisafe.go Normal file

File diff suppressed because it is too large Load Diff

60
src/handler/file.go Normal file
View File

@ -0,0 +1,60 @@
package handler
import (
"fmt"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
"time"
)
func GetTempFilename(Filename string) string {
filename := fmt.Sprintf("./uploads/%d%s", time.Now().UnixNano(), filepath.Ext(Filename))
return filename
}
func SaveFile(filename string, file multipart.File) error {
err := os.MkdirAll("./uploads", os.ModePerm)
if err != nil {
log.Panic(err)
return err
}
dst, err := os.Create(filename)
if err != nil {
log.Panic(err)
return err
}
defer dst.Close()
// Copy the uploaded file to the filesystem
// at the specified destination
_, err = io.Copy(dst, file)
if err != nil {
log.Panic(err)
return err
}
return nil
}
func DeleteFile(filePath string) error {
err := os.Remove(filePath)
if err != nil {
log.Panic(err)
return err
}
return nil
}
func DiscardFile(file multipart.File) error {
// Clear the data from memory
_, err := io.Copy(io.Discard, file)
if err != nil {
log.Panic(err)
return err
}
return nil
}