diff --git a/controllers/files.go b/controllers/files.go index 0f6ad22..4cfe3ca 100644 --- a/controllers/files.go +++ b/controllers/files.go @@ -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 @@ -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 { @@ -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 diff --git a/crontab/crontab.go b/crontab/crontab.go index 6db7c37..92dc41e 100644 --- a/crontab/crontab.go +++ b/crontab/crontab.go @@ -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 { @@ -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 { @@ -45,5 +45,4 @@ func Crontab() { }) terminator.Start() - defer terminator.Stop() } diff --git a/database/db.go b/database/db.go index 210c6ca..86b5079 100644 --- a/database/db.go +++ b/database/db.go @@ -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"` } diff --git a/doc/openapi.json b/doc/openapi.json index 6407a49..5f39a0a 100644 --- a/doc/openapi.json +++ b/doc/openapi.json @@ -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"}}}} \ No newline at end of file +{"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"}}}} \ No newline at end of file diff --git a/main.go b/main.go index 0ae9122..b35a4f4 100644 --- a/main.go +++ b/main.go @@ -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), diff --git a/utils/port.go b/utils/port.go new file mode 100644 index 0000000..8ac3677 --- /dev/null +++ b/utils/port.go @@ -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 +}