Manual server file uploads implemented

This commit is contained in:
Magnus Åhall 2026-06-26 08:31:54 +02:00
parent c8308664d3
commit 70b5285e16
10 changed files with 329 additions and 26 deletions

64
file.go
View file

@ -5,35 +5,46 @@ import (
"github.com/jmoiron/sqlx"
// Standard
"fmt"
"os"
"path"
"time"
)
type File struct {
ID int
UUID string
UserID int `db:"user_id"`
NodeID int `db:"node_id"`
Filename string
Size int64
Size int
MIME string
MD5 string
Uploaded time.Time
Modified time.Time
}
func AddFile(userID int, file *File) (err error) { // {{{
var (
fileUUIDCache map[string]int
)
func init() {
fileUUIDCache = make(map[string]int)
}
func AddFileToDb(userID int, file *File) (err error) { // {{{
file.UserID = userID
var rows *sqlx.Rows
rows, err = db.Queryx(`
INSERT INTO file(user_id, node_id, filename, size, mime, md5)
VALUES($1, $2, $3, $4, $5, $6)
INSERT INTO public.file(user_id, uuid, name, size, type, modified, upload_complete)
VALUES($1, $2, $3, $4, $5, $6, false)
ON CONFLICT (user_id, uuid) DO UPDATE set name = public.file.name
RETURNING id
`,
file.UserID,
file.NodeID,
file.UUID,
file.Filename,
file.Size,
file.MIME,
file.MD5,
file.Modified,
)
if err != nil {
return
@ -76,5 +87,40 @@ func Files(userID int, nodeUUID string, fileID int) (files []File, err error) {
return
} // }}}
func FileIDToPath(id int) (string, string) { // {{{
str := fmt.Sprintf("%012d", id)
dir := path.Join(config.Storage.Files, str[0:4], str[4:8])
fpath := path.Join(config.Storage.Files, str[0:4], str[4:8], str[8:12])
return dir, fpath
} // }}}
func FileWriteData(userID int, dbID int, data []byte, create bool) (err error) {// {{{
dir, fpath := FileIDToPath(dbID)
err = os.MkdirAll(dir, 0700)
if err != nil {
Log.Error("file", "error", err)
return
}
var f *os.File
if create {
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
} else {
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
}
if err != nil {
Log.Error("file", "error", err)
return
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
Log.Error("file", "error", err)
return
}
return
}// }}}
// vim: foldmethod=marker