Skip to content

Commit

Permalink
Update database and file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
minpeter committed Nov 30, 2023
2 parents 73c7fc9 + 8412896 commit a1bc2f0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
5 changes: 5 additions & 0 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func CreateDBEngine() error {
if err != nil {
return err
}

Engine.SetMaxIdleConns(20)
Engine.SetMaxOpenConns(20)
Engine.SetConnMaxLifetime(time.Minute * 5)

if err := Engine.Ping(); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net/url"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/tempfiles-Team/tempfiles-backend/database"
Expand Down Expand Up @@ -70,7 +69,7 @@ func DownloadHandler(c *gin.Context) {
return
}

if (FileTracking.DownloadLimit != 0 && FileTracking.DownloadCount >= FileTracking.DownloadLimit) || !FileTracking.ExpireTime.Before(time.Now()) || FileTracking.IsDeleted {
if FileTracking.DownloadLimit != 0 && FileTracking.DownloadCount >= FileTracking.DownloadLimit {

FileTracking.IsDeleted = true

Expand Down
7 changes: 6 additions & 1 deletion file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func FileHandler(c *gin.Context) {
return
}

baseUrl := c.Request.Host
scheme := "http"
if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}

baseUrl := scheme + "://" + c.Request.Host

if files, err := GetFiles(FileTracking.FolderId, baseUrl); err != nil {
c.JSON(500, gin.H{
Expand Down
2 changes: 1 addition & 1 deletion file/id_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func GenerateFolderId(files []*multipart.FileHeader) (string, error) {

combinedHash := sha1.Sum(bytes.Join(hashes, nil))

return base64.StdEncoding.EncodeToString(combinedHash[:]), nil
return base64.RawURLEncoding.EncodeToString(combinedHash[:]), nil
}
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,64 +81,70 @@ func main() {

app.GET("/info", func(c *gin.Context) {
apiName := c.Query("api")
backendUrl := c.Request.Host

scheme := "http"
if c.Request.TLS != nil || c.Request.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}

baseUrl := scheme + "://" + c.Request.Host

switch apiName {
case "upload":
c.JSON(200, gin.H{
"apiName": "/upload",
"method": "POST",
"desc": "νŠΉμ • νŒŒμΌμ„ μ„œλ²„μ— μ—…λ‘œλ“œν•©λ‹ˆλ‹€.",
"command": "curl -X POST -F 'file=@[filepath or filename]' " + backendUrl + "/upload",
"command": "curl -LX POST -F 'file=@[filepath or filename]' " + baseUrl + "/upload",
})
case "list":
c.JSON(200, gin.H{
"apiName": "/list",
"method": "GET",
"desc": "μ„œλ²„μ— μ‘΄μž¬ν•˜λŠ” 파일 리슀트λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.",
"command": "curl " + backendUrl + "/list",
"command": "curl -L " + baseUrl + "/list",
})
case "file":
c.JSON(200, gin.H{
"apiName": "/file/[file_id]",
"method": "GET",
"desc": "μ„œλ²„μ— μ‘΄μž¬ν•˜λŠ” νŠΉμ • νŒŒμΌμ— λŒ€ν•œ μ„ΈλΆ€ 정보λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.",
"command": "curl " + backendUrl + "/file/[file_id]",
"command": "curl -L " + baseUrl + "/file/[file_id]",
})
case "del":
c.JSON(200, gin.H{
"apiName": "/del/[file_id]",
"method": "DELETE",
"desc": "μ„œλ²„μ— μ‘΄μž¬ν•˜λŠ” νŠΉμ • νŒŒμΌμ„ μ‚­μ œν•©λ‹ˆλ‹€.",
"command": "curl -X DELETE " + backendUrl + "/del/[file_id]",
"command": "curl -LX DELETE " + baseUrl + "/del/[file_id]",
})
case "dl":
c.JSON(200, gin.H{
"apiName": "/dl/[file_id]",
"method": "GET",
"desc": "μ„œλ²„μ— μ‘΄μž¬ν•˜λŠ” νŠΉμ • νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ ν•©λ‹ˆλ‹€.",
"command": "curl -O " + backendUrl + "/dl/[file_id]",
"command": "curl -LO " + baseUrl + "/dl/[file_id]",
})
default:
c.JSON(200, []gin.H{
{
"apiUrl": backendUrl + "/upload",
"apiUrl": baseUrl + "/upload",
"apiHandler": "upload",
},
{

"apiUrl": backendUrl + "/list",
"apiUrl": baseUrl + "/list",
"apiHandler": "list",
},
{
"apiUrl": backendUrl + "/file/[file_id]",
"apiUrl": baseUrl + "/file/[file_id]",
"apiHandler": "file",
},
{
"apiUrl": backendUrl + "/del/[file_id]",
"apiUrl": baseUrl + "/del/[file_id]",
"apiHandler": "del",
},
{
"apiUrl": backendUrl + "/dl/[file_id]",
"apiUrl": baseUrl + "/dl/[file_id]",
"apiHandler": "dl",
},
})
Expand Down

0 comments on commit a1bc2f0

Please sign in to comment.