This repository was archived by the owner on Sep 2, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 4
4
"fmt"
5
5
"net/http"
6
6
"path/filepath"
7
+ "regexp"
8
+ "strings"
7
9
"time"
8
10
9
11
"github.com/staticbackendhq/core/internal"
@@ -37,11 +39,10 @@ func upload(w http.ResponseWriter, r *http.Request) {
37
39
38
40
ext := filepath .Ext (h .Filename )
39
41
40
- //TODO: Remove all but a-zA-Z/ from name
41
-
42
42
name := r .Form .Get ("name" )
43
43
if len (name ) == 0 {
44
- name = randStringRunes (32 )
44
+ // if no forced name is used, let's use the original name
45
+ name = cleanUpFileName (h .Filename )
45
46
}
46
47
47
48
fileKey := fmt .Sprintf ("%s/%s/%s%s" ,
@@ -110,3 +111,13 @@ func deleteFile(w http.ResponseWriter, r *http.Request) {
110
111
111
112
respond (w , http .StatusOK , true )
112
113
}
114
+
115
+ // cleanUpFileName removes file extention and anything but a-zA-Z-_
116
+ func cleanUpFileName (s string ) string {
117
+ s = strings .TrimSuffix (s , filepath .Ext (s ))
118
+
119
+ exp := regexp .MustCompile (`[^a-zA-Z\-_]` )
120
+
121
+ return exp .ReplaceAllString (s , "" )
122
+
123
+ }
Original file line number Diff line number Diff line change
1
+ package staticbackend
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ func TestCleanUpFileName (t * testing.T ) {
8
+ fakeNames := make (map [string ]string )
9
+ fakeNames ["" ] = ""
10
+ fakeNames ["abc.def" ] = "abc"
11
+ fakeNames ["ok!.test" ] = "ok"
12
+ fakeNames ["@file-name_here!.ext" ] = "file-name_here"
13
+
14
+ for k , v := range fakeNames {
15
+ if clean := cleanUpFileName (k ); clean != v {
16
+ t .Errorf ("expected %s got %s" , v , clean )
17
+ }
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments