Skip to content

Commit

Permalink
timer repair, reset counter when uploading duplicates, fix port dupli…
Browse files Browse the repository at this point in the history
…cation issues
  • Loading branch information
minpeter committed Apr 22, 2024
1 parent 6a6986e commit 8c4172a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
30 changes: 27 additions & 3 deletions controllers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,32 @@ func (s RealFilesService) CreateFiles(c *fuego.ContextWithBody[any]) (File, erro
}, nil
}

FileTracking.ExpireTime = expireTimeDate.Unix()
if _, err := database.Engine.ID(FileTracking.Id).Cols("expire_time").Update(&FileTracking); err != nil {
resp := File{
Error: err.Error(),
Message: fmt.Sprintf("database update error: %v", err),
}

resp.FileTracking = FileTracking

return resp, nil
}

FileTracking.DownloadCount = 0
if _, err := database.Engine.ID(FileTracking.Id).Cols("download_count").Update(&FileTracking); err != nil {
resp := File{
Error: err.Error(),
Message: fmt.Sprintf("database update error: %v", err),
}

resp.FileTracking = FileTracking

return resp, nil
}

resp := File{
Message: fmt.Sprintf("File %s already exists", FileTracking.FolderHash),
Message: fmt.Sprintf("File %s already exists, reset time limit and download count.", FileTracking.FolderHash[:5]),
}

resp.FileTracking = FileTracking
Expand All @@ -304,7 +328,7 @@ func (s RealFilesService) CreateFiles(c *fuego.ContextWithBody[any]) (File, erro
FolderHash: FolderHash,
UploadDate: time.Now(),
DownloadLimit: int64(downloadLimit),
ExpireTime: expireTimeDate,
ExpireTime: expireTimeDate.Unix(),
}

if utils.CheckFileFolder(FileTracking.FolderId) != nil {
Expand Down Expand Up @@ -339,7 +363,7 @@ func (s RealFilesService) CreateFiles(c *fuego.ContextWithBody[any]) (File, erro
log.Printf("🥰 Successfully uploaded %s, %d files\n", FileTracking.FolderId, FileTracking.FileCount)

resp := File{
Message: fmt.Sprintf("File %s uploaded successfully", FileTracking.FolderHash),
Message: fmt.Sprintf("File %s uploaded successfully", FileTracking.FolderHash[:5]),
}

resp.FileTracking = *FileTracking
Expand Down
7 changes: 3 additions & 4 deletions crontab/crontab.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
func Crontab() {
terminator := cron.New()

terminator.AddFunc("1 */5 * * *", func() {
terminator.AddFunc("@every 30s", func() {
log.Println("⏲️ Check for expired files", time.Now().Format("2006-01-02 15:04:05"))
var files []database.FileTracking
if err := database.Engine.Where("expire_time < ? and is_deleted = ?", time.Now(), false).Find(&files); err != nil {
if err := database.Engine.Where("expire_time < ? and is_deleted = ?", time.Now().Unix(), false).Find(&files); err != nil {
log.Println("cron db query error", err.Error())
}
for _, file := range files {
Expand All @@ -27,7 +27,7 @@ func Crontab() {
}
})

terminator.AddFunc("1 */20 * * *", func() {
terminator.AddFunc("@every 1m", func() {
log.Println("⏲️ Check which files need to be deleted", time.Now().Format("2006-01-02 15:04:05"))
var files []database.FileTracking
if err := database.Engine.Where("is_deleted = ?", true).Find(&files); err != nil {
Expand All @@ -45,5 +45,4 @@ func Crontab() {
})

terminator.Start()
defer terminator.Stop()
}
2 changes: 1 addition & 1 deletion database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type FileTracking struct {
DownloadCount int64 `json:"downloadCount"`
DownloadLimit int64 `json:"downloadLimit"`
UploadDate time.Time `json:"uploadDate"`
ExpireTime time.Time `json:"expireTime"`
ExpireTime int64 `json:"expireTime"`

Files []FileListResponse `json:"files"`
}
Expand Down
2 changes: 1 addition & 1 deletion doc/openapi.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"components":{"schemas":{"File":{"properties":{"downloadCount":{"format":"int64","type":"integer"},"downloadLimit":{"format":"int64","type":"integer"},"error":{"type":"string"},"expireTime":{"format":"date-time","type":"string"},"fileCount":{"type":"integer"},"files":{"items":{"properties":{"downloadUrl":{"type":"string"},"fileName":{"type":"string"},"fileSize":{"format":"int64","type":"integer"}},"type":"object"},"type":"array"},"folderId":{"type":"string"},"isHidden":{"type":"boolean"},"message":{"type":"string"},"uploadDate":{"format":"date-time","type":"string"}},"type":"object"},"Files":{"properties":{"error":{"type":"string"},"list":{"items":{"properties":{"downloadCount":{"format":"int64","type":"integer"},"downloadLimit":{"format":"int64","type":"integer"},"expireTime":{"format":"date-time","type":"string"},"fileCount":{"type":"integer"},"files":{"items":{"properties":{"downloadUrl":{"type":"string"},"fileName":{"type":"string"},"fileSize":{"format":"int64","type":"integer"}},"type":"object"},"type":"array"},"folderId":{"type":"string"},"isHidden":{"type":"boolean"},"uploadDate":{"format":"date-time","type":"string"}},"type":"object"},"type":"array"},"message":{"type":"string"}},"type":"object"},"string":{"type":"string"},"unknown-interface":{}}},"info":{"description":"OpenAPI","title":"OpenAPI","version":"0.0.1"},"openapi":"3.0.3","paths":{"/":{"get":{"description":"controller: main.main.func1","operationId":"GET /:func1","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/string"}}},"description":"OK"},"default":{"description":""}},"summary":"func1","tags":["default"]}},"/del/{id}":{"delete":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.deleteFiles","operationId":"DELETE /del/{id}:deleteFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"deleteFiles","tags":["File"]}},"/dl/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.downloadFile","operationId":"GET /dl/{id}/{name}:downloadFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"downloadFile"}},"/file/{id}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getFiles","operationId":"GET /file/{id}:getFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"getFiles","tags":["File"]}},"/list":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getAllFiles","operationId":"GET /list:getAllFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getAllFiles","tags":["Files"]}},"/upload":{"post":{"description":"Upload files","operationId":"POST /upload:postFiles","parameters":[{"description":"Download limit","in":"header","name":"X-Download-Limit","schema":{"type":"string"}},{"description":"Time limit","in":"header","name":"X-Time-Limit","schema":{"type":"string"}},{"description":"Hidden","in":"header","name":"X-Hidden","schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"postFiles","tags":["File"]}},"/view/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.ViewFile","operationId":"GET /view/{id}/{name}:ViewFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"ViewFile"}}}}
{"components":{"schemas":{"File":{"properties":{"downloadCount":{"format":"int64","type":"integer"},"downloadLimit":{"format":"int64","type":"integer"},"error":{"type":"string"},"expireTime":{"format":"int64","type":"integer"},"fileCount":{"type":"integer"},"files":{"items":{"properties":{"downloadUrl":{"type":"string"},"fileName":{"type":"string"},"fileSize":{"format":"int64","type":"integer"}},"type":"object"},"type":"array"},"folderId":{"type":"string"},"isHidden":{"type":"boolean"},"message":{"type":"string"},"uploadDate":{"format":"date-time","type":"string"}},"type":"object"},"Files":{"properties":{"error":{"type":"string"},"list":{"items":{"properties":{"downloadCount":{"format":"int64","type":"integer"},"downloadLimit":{"format":"int64","type":"integer"},"expireTime":{"format":"int64","type":"integer"},"fileCount":{"type":"integer"},"files":{"items":{"properties":{"downloadUrl":{"type":"string"},"fileName":{"type":"string"},"fileSize":{"format":"int64","type":"integer"}},"type":"object"},"type":"array"},"folderId":{"type":"string"},"isHidden":{"type":"boolean"},"uploadDate":{"format":"date-time","type":"string"}},"type":"object"},"type":"array"},"message":{"type":"string"}},"type":"object"},"string":{"type":"string"},"unknown-interface":{}}},"info":{"description":"OpenAPI","title":"OpenAPI","version":"0.0.1"},"openapi":"3.0.3","paths":{"/":{"get":{"description":"controller: main.main.func1","operationId":"GET /:func1","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/string"}}},"description":"OK"},"default":{"description":""}},"summary":"func1","tags":["default"]}},"/del/{id}":{"delete":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.deleteFiles","operationId":"DELETE /del/{id}:deleteFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"deleteFiles","tags":["File"]}},"/dl/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.downloadFile","operationId":"GET /dl/{id}/{name}:downloadFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"downloadFile"}},"/file/{id}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getFiles","operationId":"GET /file/{id}:getFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"getFiles","tags":["File"]}},"/list":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getAllFiles","operationId":"GET /list:getAllFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getAllFiles","tags":["Files"]}},"/upload":{"post":{"description":"Upload files","operationId":"POST /upload:postFiles","parameters":[{"description":"Download limit","in":"header","name":"X-Download-Limit","schema":{"type":"string"}},{"description":"Time limit","in":"header","name":"X-Time-Limit","schema":{"type":"string"}},{"description":"Hidden","in":"header","name":"X-Hidden","schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/File"}}},"description":"OK"},"default":{"description":""}},"summary":"postFiles","tags":["File"]}},"/view/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.ViewFile","operationId":"GET /view/{id}/{name}:ViewFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"ViewFile"}}}}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
if os.Getenv("BACKEND_PORT") == "" {
os.Setenv("BACKEND_PORT", "5000")
}
port := os.Getenv("BACKEND_PORT")
port := utils.CheckPortAvailable(os.Getenv("BACKEND_PORT"))

s := fuego.NewServer(
fuego.WithAddr("0.0.0.0:"+port),
Expand Down
33 changes: 33 additions & 0 deletions utils/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"fmt"
"net"
"strconv"
)

func CheckPortAvailable(port string) string {
// Parse the port number
p, err := strconv.Atoi(port)
if err != nil {
return ""
}

// Check if the port is available
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", p))
if err != nil {
return ""
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
// If the port is not available, increment the port number and try again
return CheckPortAvailable(fmt.Sprintf("%d", p+1))
}
defer l.Close()

// If the port is available, return it

fmt.Println("Port is available:", port)
return port
}

0 comments on commit 8c4172a

Please sign in to comment.