-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
55 lines (48 loc) · 1.65 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"path"
"path/filepath"
)
func (app *Application) apiList(c *gin.Context) {
if err := app.apiListTemplate.Execute(c.Writer, c.Request.Host); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}
func (app *Application) indexPage(c *gin.Context) {
if err := app.indexTemplate.Execute(c.Writer, c.Request.Host); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
}
}
func (app *Application) indexFiles(c *gin.Context) {
c.Status(http.StatusOK)
// Looks if file exists in public folder then redirects there
filePath := filepath.Join("public", path.Clean(c.Request.URL.Path))
if file, err := publicFiles.Open(filePath); err == nil {
file.Close()
c.Redirect(http.StatusPermanentRedirect, path.Join("public", path.Clean(c.Request.URL.Path)))
return
}
// Looks in database for uploaded file
if exists, err := app.db.fileExists(path.Base(path.Clean(c.Request.URL.Path))); err != nil {
app.logError.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
} else if !exists {
c.Redirect(http.StatusTemporaryRedirect, "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
return
}
switch app.config.fileStorageMethod {
case fileStorageS3:
c.Redirect(http.StatusTemporaryRedirect, "https://"+app.config.S3.CdnDomain+"/file/"+app.config.S3.Bucket+path.Clean(c.Request.URL.Path))
case fileStorageLocal:
c.File(filepath.Join(app.config.DataFolder, path.Clean(c.Request.URL.Path)))
default:
app.logError.Println(ErrUnknownStorageMethod)
c.AbortWithStatus(http.StatusInternalServerError)
}
}