@@ -86,36 +86,55 @@ func UploadUserFiles(router *gin.RouterGroup) {
86
86
return
87
87
}
88
88
89
- // List of allowed file extensions (all types allowed if empty).
90
- allowExt := conf .UploadAllow ()
89
+ // If the file extension list is empty, all file types may
90
+ // be uploaded except raw files if raw support is disabled.
91
+ allowedExt := conf .UploadAllow ()
92
+ rejectRaw := conf .DisableRaw ()
91
93
92
- // Save uploaded files if their extension is allowed.
94
+ // Save uploaded files and append their names
95
+ // to "uploads" if they pass all checks.
93
96
for _ , file := range files {
94
- fileName := filepath .Base (file .Filename )
95
- filePath := path .Join (uploadDir , fileName )
96
- fileType := fs .FileType (fileName )
97
+ baseName := filepath .Base (file .Filename )
98
+ destName := path .Join (uploadDir , baseName )
99
+ fileType := fs .FileType (baseName )
97
100
101
+ // Reject unsupported files and files with extensions that aren't allowed.
98
102
if fileType == fs .TypeUnknown {
99
- log .Warnf ("upload: rejected %s due to unknown or unsupported extension" , clean .Log (fileName ))
103
+ log .Warnf ("upload: rejected %s because it has an unsupported file extension" , clean .Log (baseName ))
100
104
continue
101
- } else if allowExt .Excludes (fileType .DefaultExt ()) {
102
- log .Warnf ("upload: rejected %s because the file type is not allowed" , clean .Log (fileName ))
105
+ } else if allowedExt .Excludes (fileType .DefaultExt ()) {
106
+ log .Warnf ("upload: rejected %s because its extension is not allowed" , clean .Log (baseName ))
103
107
continue
104
108
}
105
109
106
- if err = c .SaveUploadedFile (file , filePath ); err != nil {
107
- log .Errorf ("upload: failed saving file %s" , clean .Log (fileName ))
110
+ // Save uploaded file in the user upload path.
111
+ if err = c .SaveUploadedFile (file , destName ); err != nil {
112
+ log .Errorf ("upload: failed to save %s" , clean .Log (baseName ))
113
+ log .Debugf ("upload: %s in %s" , clean .Error (err ), clean .Log (baseName ))
108
114
Abort (c , http .StatusBadRequest , i18n .ErrUploadFailed )
109
115
return
110
116
} else {
111
- log .Debugf ("upload: saved file %s " , clean .Log (fileName ))
112
- event .Publish ("upload.saved" , event.Data {"uid" : s .UserUID , "file" : fileName })
117
+ log .Debugf ("upload: saved %s in user upload path " , clean .Log (baseName ))
118
+ event .Publish ("upload.saved" , event.Data {"uid" : s .UserUID , "file" : baseName })
113
119
}
114
120
115
- uploads = append (uploads , filePath )
121
+ // Make sure the file is supported and has the correct extension before importing it.
122
+ if mediaFile , mediaErr := photoprism .NewMediaFile (destName ); mediaErr != nil {
123
+ log .Errorf ("upload: rejected %s, %s" , clean .Error (err ), clean .Log (baseName ))
124
+ logErr ("upload" , os .Remove (destName ))
125
+ } else if typeErr := mediaFile .CheckType (); typeErr != nil {
126
+ log .Warnf ("upload: rejected %s %s" , clean .Log (baseName ), typeErr )
127
+ logErr ("upload" , os .Remove (destName ))
128
+ } else if rejectRaw && mediaFile .IsRaw () {
129
+ log .Warnf ("upload: rejected %s because raw support is disabled" , clean .Log (baseName ))
130
+ logErr ("upload" , os .Remove (destName ))
131
+ } else {
132
+ // Successfully validated upload.
133
+ uploads = append (uploads , destName )
134
+ }
116
135
}
117
136
118
- // Check if uploaded file is safe .
137
+ // Check if the uploaded file may contain inappropriate content .
119
138
if len (uploads ) > 0 && ! conf .UploadNSFW () {
120
139
nd := get .NsfwDetector ()
121
140
@@ -152,6 +171,7 @@ func UploadUserFiles(router *gin.RouterGroup) {
152
171
153
172
elapsed := int (time .Since (start ).Seconds ())
154
173
174
+ // Log number of successfully uploaded files.
155
175
msg := i18n .Msg (i18n .MsgFilesUploadedIn , len (uploads ), elapsed )
156
176
157
177
log .Info (msg )
@@ -240,7 +260,7 @@ func ProcessUserUpload(router *gin.RouterGroup) {
240
260
}
241
261
242
262
// Update moments if files have been imported.
243
- if n := len ( imported ); n == 0 {
263
+ if n := imported . Processed ( ); n == 0 {
244
264
log .Infof ("upload: found no new files to import from %s" , clean .Log (uploadPath ))
245
265
} else {
246
266
log .Infof ("upload: imported %s" , english .Plural (n , "file" , "files" ))
0 commit comments