@@ -3,12 +3,9 @@ package main
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
- "os"
7
- "path/filepath"
8
6
"strconv"
9
7
10
- "github.com/disintegration/imaging"
11
- "github.com/knadh/listmonk/models"
8
+ "github.com/knadh/listmonk/media"
12
9
"github.com/labstack/echo"
13
10
uuid "github.com/satori/go.uuid"
14
11
)
@@ -26,53 +23,72 @@ func handleUploadMedia(c echo.Context) error {
26
23
app = c .Get ("app" ).(* App )
27
24
cleanUp = false
28
25
)
29
-
26
+ file , err := c .FormFile ("file" )
27
+ if err != nil {
28
+ return echo .NewHTTPError (http .StatusBadRequest ,
29
+ fmt .Sprintf ("Invalid file uploaded: %v" , err ))
30
+ }
31
+ // Validate MIME type with the list of allowed types.
32
+ var typ = file .Header .Get ("Content-type" )
33
+ ok := validateMIME (typ , imageMimes )
34
+ if ! ok {
35
+ return echo .NewHTTPError (http .StatusBadRequest ,
36
+ fmt .Sprintf ("Unsupported file type (%s) uploaded." , typ ))
37
+ }
38
+ // Generate filename
39
+ fName := generateFileName (file .Filename )
40
+ // Read file contents in memory
41
+ src , err := file .Open ()
42
+ if err != nil {
43
+ return echo .NewHTTPError (http .StatusBadRequest ,
44
+ fmt .Sprintf ("Error reading file: %s" , err ))
45
+ }
46
+ defer src .Close ()
30
47
// Upload the file.
31
- fName , err := uploadFile ( "file" , app .Constants . UploadPath , "" , imageMimes , c )
48
+ fName , err = app .Media . Put ( fName , typ , src )
32
49
if err != nil {
50
+ cleanUp = true
33
51
return echo .NewHTTPError (http .StatusInternalServerError ,
34
52
fmt .Sprintf ("Error uploading file: %s" , err ))
35
53
}
36
- path := filepath .Join (app .Constants .UploadPath , fName )
37
54
38
55
defer func () {
39
56
// If any of the subroutines in this function fail,
40
57
// the uploaded image should be removed.
41
58
if cleanUp {
42
- os .Remove (path )
59
+ app .Media .Delete (fName )
60
+ app .Media .Delete (thumbPrefix + fName )
43
61
}
44
62
}()
45
63
46
- // Create a thumbnail.
47
- src , err := imaging . Open ( path )
64
+ // Create thumbnail from file .
65
+ thumbFile , err := createThumbnail ( file )
48
66
if err != nil {
49
67
cleanUp = true
50
68
return echo .NewHTTPError (http .StatusInternalServerError ,
51
69
fmt .Sprintf ("Error opening image for resizing: %s" , err ))
52
70
}
53
-
54
- t := imaging . Resize ( src , thumbnailSize , 0 , imaging . Lanczos )
55
- if err := imaging . Save ( t , fmt . Sprintf ( "%s/%s%s" , app . Constants . UploadPath , thumbPrefix , fName )); err != nil {
71
+ // Upload thumbnail.
72
+ thumbfName , err := app . Media . Put ( thumbPrefix + fName , typ , thumbFile )
73
+ if err != nil {
56
74
cleanUp = true
57
75
return echo .NewHTTPError (http .StatusInternalServerError ,
58
76
fmt .Sprintf ("Error saving thumbnail: %s" , err ))
59
77
}
60
-
61
78
// Write to the DB.
62
- if _ , err := app .Queries .InsertMedia .Exec (uuid .NewV4 (), fName , fmt . Sprintf ( "%s%s" , thumbPrefix , fName ) , 0 , 0 ); err != nil {
79
+ if _ , err := app .Queries .InsertMedia .Exec (uuid .NewV4 (), fName , thumbfName , 0 , 0 ); err != nil {
63
80
cleanUp = true
64
81
return echo .NewHTTPError (http .StatusInternalServerError ,
65
- fmt .Sprintf ("Error saving uploaded file: %s" , pqErrMsg (err )))
82
+ fmt .Sprintf ("Error saving uploaded file to db : %s" , pqErrMsg (err )))
66
83
}
67
-
68
84
return c .JSON (http .StatusOK , okResp {true })
69
85
}
70
86
71
87
// handleGetMedia handles retrieval of uploaded media.
72
88
func handleGetMedia (c echo.Context ) error {
73
89
var (
74
90
app = c .Get ("app" ).(* App )
75
- out []models .Media
91
+ out []media .Media
76
92
)
77
93
78
94
if err := app .Queries .GetMedia .Select (& out ); err != nil {
@@ -81,8 +97,8 @@ func handleGetMedia(c echo.Context) error {
81
97
}
82
98
83
99
for i := 0 ; i < len (out ); i ++ {
84
- out [i ].URI = fmt . Sprintf ( "%s/%s" , app .Constants . UploadURI , out [i ].Filename )
85
- out [i ].ThumbURI = fmt . Sprintf ( "%s/%s%s" , app .Constants . UploadURI , thumbPrefix , out [i ].Filename )
100
+ out [i ].URI = app .Media . Get ( out [i ].Filename )
101
+ out [i ].ThumbURI = app .Media . Get ( thumbPrefix + out [i ].Filename )
86
102
}
87
103
88
104
return c .JSON (http .StatusOK , okResp {out })
@@ -99,13 +115,14 @@ func handleDeleteMedia(c echo.Context) error {
99
115
return echo .NewHTTPError (http .StatusBadRequest , "Invalid ID." )
100
116
}
101
117
102
- var m models .Media
118
+ var m media .Media
103
119
if err := app .Queries .DeleteMedia .Get (& m , id ); err != nil {
104
120
return echo .NewHTTPError (http .StatusInternalServerError ,
105
121
fmt .Sprintf ("Error deleting media: %s" , pqErrMsg (err )))
106
122
}
107
- os .Remove (filepath .Join (app .Constants .UploadPath , m .Filename ))
108
- os .Remove (filepath .Join (app .Constants .UploadPath , fmt .Sprintf ("%s%s" , thumbPrefix , m .Filename )))
123
+
124
+ app .Media .Delete (m .Filename )
125
+ app .Media .Delete (thumbPrefix + m .Filename )
109
126
110
127
return c .JSON (http .StatusOK , okResp {true })
111
128
}
0 commit comments