commit ebca59dcac218f4a53470fea456141ddf7a20844
parent 4f1330ae09300da0af87ae07c319b5feb030d29c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 26 Jan 2023 11:54:39 -0800
cleanup uploads/filedrop folders, have them in the project home subdirectory (.dkf)
Diffstat:
6 files changed, 61 insertions(+), 23 deletions(-)
diff --git a/filedrop/empty b/filedrop/empty
diff --git a/pkg/actions/actions.go b/pkg/actions/actions.go
@@ -155,6 +155,16 @@ func ensureProjectHome() {
if err := os.MkdirAll(config.Global.ProjectHTMLPath(), 0755); err != nil {
logrus.Fatal("Failed to create dkforest html folder", err)
}
+
+ config.Global.SetProjectUploadsPath(filepath.Join(projectPath, "uploads"))
+ if err := os.MkdirAll(config.Global.ProjectUploadsPath(), 0755); err != nil {
+ logrus.Fatal("Failed to create dkforest uploads folder", err)
+ }
+
+ config.Global.SetProjectFiledropPath(filepath.Join(projectPath, "filedrop"))
+ if err := os.MkdirAll(config.Global.ProjectFiledropPath(), 0755); err != nil {
+ logrus.Fatal("Failed to create dkforest filedrop folder", err)
+ }
}
// LogFormatter ...
diff --git a/pkg/config/config.go b/pkg/config/config.go
@@ -153,15 +153,17 @@ var ConnMap = NewConnManager()
// GlobalConf ...
type GlobalConf struct {
sync.RWMutex
- appVersion *version.Version
- projectPath string // project path
- projectLocalsPath string // directory where we keep custom translation files
- projectHtmlPath string
- sha string
- masterKey string
- cookieSecure bool
- cookieDomain string
- baseURL string // (http://127.0.0.1:8080)
+ appVersion *version.Version
+ projectPath string // project path
+ projectLocalsPath string // directory where we keep custom translation files
+ projectHtmlPath string
+ projectUploadsPath string
+ projectFiledropPath string
+ sha string
+ masterKey string
+ cookieSecure bool
+ cookieDomain string
+ baseURL string // (http://127.0.0.1:8080)
}
// NewGlobalConf ...
@@ -283,6 +285,34 @@ func (c *GlobalConf) SetProjectHTMLPath(projectHtmlPath string) {
c.projectHtmlPath = projectHtmlPath
}
+// ProjectUploadsPath ...
+func (c *GlobalConf) ProjectUploadsPath() string {
+ c.RLock()
+ defer c.RUnlock()
+ return c.projectUploadsPath
+}
+
+// SetProjectUploadsPath ...
+func (c *GlobalConf) SetProjectUploadsPath(projectUploadsPath string) {
+ c.Lock()
+ defer c.Unlock()
+ c.projectUploadsPath = projectUploadsPath
+}
+
+// ProjectFiledropPath ...
+func (c *GlobalConf) ProjectFiledropPath() string {
+ c.RLock()
+ defer c.RUnlock()
+ return c.projectFiledropPath
+}
+
+// SetProjectFiledropPath ...
+func (c *GlobalConf) SetProjectFiledropPath(projectFiledropPath string) {
+ c.Lock()
+ defer c.Unlock()
+ c.projectFiledropPath = projectFiledropPath
+}
+
// BaseURL ...
func (c *GlobalConf) BaseURL() string {
c.RLock()
diff --git a/pkg/database/tableFiledrops.go b/pkg/database/tableFiledrops.go
@@ -1,6 +1,7 @@
package database
import (
+ "dkforest/pkg/config"
"dkforest/pkg/utils"
ucrypto "dkforest/pkg/utils/crypto"
"github.com/google/uuid"
@@ -10,8 +11,6 @@ import (
"time"
)
-const FiledropFolder = "filedrop"
-
type Filedrop struct {
ID int64
UUID string
@@ -46,12 +45,12 @@ func CreateFiledrop() (out Filedrop, err error) {
}
func (d *Filedrop) Exists() bool {
- filePath1 := filepath.Join(FiledropFolder, d.FileName)
+ filePath1 := filepath.Join(config.Global.ProjectFiledropPath(), d.FileName)
return utils.FileExists(filePath1)
}
func (d *Filedrop) GetContent() (*os.File, *ucrypto.StreamDecrypter, error) {
- filePath1 := filepath.Join(FiledropFolder, d.FileName)
+ filePath1 := filepath.Join(config.Global.ProjectFiledropPath(), d.FileName)
f, err := os.Open(filePath1)
if err != nil {
return nil, nil, err
@@ -66,7 +65,7 @@ func (d *Filedrop) GetContent() (*os.File, *ucrypto.StreamDecrypter, error) {
func (d *Filedrop) Delete() error {
if d.FileName != "" {
- if err := os.Remove(filepath.Join(FiledropFolder, d.FileName)); err != nil {
+ if err := os.Remove(filepath.Join(config.Global.ProjectFiledropPath(), d.FileName)); err != nil {
logrus.Error(err)
}
}
diff --git a/pkg/database/tableUploads.go b/pkg/database/tableUploads.go
@@ -1,6 +1,7 @@
package database
import (
+ "dkforest/pkg/config"
"dkforest/pkg/utils"
"io"
"io/ioutil"
@@ -11,8 +12,6 @@ import (
"github.com/sirupsen/logrus"
)
-const UploadFolder = "uploads"
-
type UploadID int64
type Upload struct {
@@ -27,7 +26,7 @@ type Upload struct {
}
func (u *Upload) GetContent() (os.FileInfo, []byte, error) {
- filePath1 := filepath.Join(UploadFolder, u.FileName)
+ filePath1 := filepath.Join(config.Global.ProjectUploadsPath(), u.FileName)
f, err := os.Open(filePath1)
if err != nil {
return nil, nil, err
@@ -47,12 +46,12 @@ func (u *Upload) GetContent() (os.FileInfo, []byte, error) {
}
func (u *Upload) Exists() bool {
- filePath1 := filepath.Join(UploadFolder, u.FileName)
+ filePath1 := filepath.Join(config.Global.ProjectUploadsPath(), u.FileName)
return utils.FileExists(filePath1)
}
func (u *Upload) Delete() error {
- if err := os.Remove(filepath.Join(UploadFolder, u.FileName)); err != nil {
+ if err := os.Remove(filepath.Join(config.Global.ProjectUploadsPath(), u.FileName)); err != nil {
return err
}
if err := DB.Delete(&u).Error; err != nil {
@@ -76,7 +75,7 @@ func CreateEncryptedUploadWithSize(fileName string, content []byte, userID UserI
func createUploadWithSize(fileName string, content []byte, userID UserID, size int64) (*Upload, error) {
newFileName := utils.MD5([]byte(utils.GenerateToken32()))
- if err := ioutil.WriteFile(filepath.Join(UploadFolder, newFileName), content, 0644); err != nil {
+ if err := ioutil.WriteFile(filepath.Join(config.Global.ProjectUploadsPath(), newFileName), content, 0644); err != nil {
return nil, err
}
upload := Upload{
@@ -123,7 +122,7 @@ func DeleteOldUploads() {
if err := DB.Exec(`DELETE FROM uploads WHERE created_at < date('now', '-1 Day')`).Error; err != nil {
logrus.Error(err.Error())
}
- fileInfo, err := ioutil.ReadDir("uploads")
+ fileInfo, err := ioutil.ReadDir(config.Global.ProjectUploadsPath())
if err != nil {
logrus.Error(err.Error())
return
@@ -131,7 +130,7 @@ func DeleteOldUploads() {
now := time.Now()
for _, info := range fileInfo {
if diff := now.Sub(info.ModTime()); diff > 24*time.Hour {
- if err := os.Remove(filepath.Join(UploadFolder, info.Name())); err != nil {
+ if err := os.Remove(filepath.Join(config.Global.ProjectUploadsPath(), info.Name())); err != nil {
logrus.Error(err.Error())
}
}
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -4211,7 +4211,7 @@ func FileDropHandler(c echo.Context) error {
return c.Render(http.StatusOK, filedropTmplName, data)
}
- outFile, err := os.OpenFile(filepath.Join(database.FiledropFolder, filedrop.FileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ outFile, err := os.OpenFile(filepath.Join(config.Global.ProjectFiledropPath(), filedrop.FileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
data.Error = err.Error()
return c.Render(http.StatusOK, filedropTmplName, data)