@@ -8,13 +8,15 @@ import (
8
8
"files/pkg/files"
9
9
"files/pkg/postgres"
10
10
"fmt"
11
- "github.com/spf13/afero"
12
- "gorm.io/gorm"
13
11
"io/ioutil"
14
- "k8s.io/klog/v2"
15
12
"net/http"
16
13
"strconv"
14
+ "strings"
17
15
"time"
16
+
17
+ "github.com/spf13/afero"
18
+ "gorm.io/gorm"
19
+ "k8s.io/klog/v2"
18
20
)
19
21
20
22
type ShareablePutRequestBody struct {
@@ -197,8 +199,50 @@ func shareLinkGetHandler(w http.ResponseWriter, r *http.Request, d *common.Data)
197
199
return http .StatusInternalServerError , err
198
200
}
199
201
202
+ result := map [string ]interface {}{
203
+ "code" : 0 ,
204
+ "message" : "success" ,
205
+ "data" : map [string ]interface {}{
206
+ "count" : len (shareLinks ),
207
+ "items" : shareLinks ,
208
+ },
209
+ }
200
210
w .Header ().Set ("Content-Type" , "application/json" )
201
- return common .RenderJSON (w , r , shareLinks )
211
+ return common .RenderJSON (w , r , result )
212
+ }
213
+
214
+ func useShareLinkGetHandler (w http.ResponseWriter , r * http.Request , d * common.Data ) (int , error ) {
215
+ password := r .URL .Query ().Get ("password" )
216
+ if password == "" {
217
+ return http .StatusBadRequest , nil
218
+ }
219
+
220
+ pathMD5 := strings .Trim (r .URL .Path , "/" )
221
+ var shareLink postgres.ShareLink
222
+ err := postgres .DBServer .Where ("path_md5 = ? AND password = ?" , pathMD5 , common .Md5String (password )).First (& shareLink ).Error
223
+ if err != nil {
224
+ if errors .Is (err , gorm .ErrRecordNotFound ) {
225
+ return http .StatusNotFound , fmt .Errorf ("share link not found" )
226
+ } else {
227
+ return http .StatusInternalServerError , fmt .Errorf ("failed to query share link: %v" , err )
228
+ }
229
+ }
230
+
231
+ result := map [string ]interface {}{
232
+ "code" : 0 ,
233
+ "message" : "success" ,
234
+ // TODO: generate a new token
235
+ "token" : "" ,
236
+ "data" : map [string ]interface {}{
237
+ "permission" : shareLink .Permission ,
238
+ "expire_in" : shareLink .ExpireIn ,
239
+ "paths" : []string {shareLink .Path },
240
+ "owner_id" : shareLink .OwnerID ,
241
+ "owner_name" : shareLink .OwnerName ,
242
+ },
243
+ }
244
+
245
+ return common .RenderJSON (w , r , result )
202
246
}
203
247
204
248
type ShareLinkPostRequestBody struct {
@@ -275,11 +319,12 @@ func shareLinkPostHandler(w http.ResponseWriter, r *http.Request, d *common.Data
275
319
276
320
// Calculate expire time
277
321
expireTime := time .Now ().Add (time .Duration (requestBody .ExpireIn ) * time .Millisecond )
278
-
322
+ pathMD5 := common . Md5String ( r . URL . Path + fmt . Sprint ( time . Now (). UnixNano ()))
279
323
newShareLink := postgres.ShareLink {
280
- LinkURL : host + "/share_link/" + common . Md5String ( r . URL . Path + fmt . Sprint ( time . Now (). UnixNano ())) ,
324
+ LinkURL : host + "/share_link/" + pathMD5 ,
281
325
PathID : pathID ,
282
326
Path : r .URL .Path ,
327
+ PathMD5 : pathMD5 ,
283
328
Password : common .Md5String (requestBody .Password ),
284
329
OwnerID : ownerID ,
285
330
OwnerName : ownerName ,
@@ -297,8 +342,12 @@ func shareLinkPostHandler(w http.ResponseWriter, r *http.Request, d *common.Data
297
342
return http .StatusInternalServerError , result .Error
298
343
}
299
344
345
+ w .Header ().Set ("Content-Type" , "application/json" )
300
346
w .WriteHeader (http .StatusCreated )
301
- return http .StatusOK , nil
347
+ jsonData := map [string ]string {
348
+ "share_link" : newShareLink .LinkURL ,
349
+ }
350
+ return common .RenderJSON (w , r , jsonData )
302
351
}
303
352
304
353
func shareLinkDeleteHandler (w http.ResponseWriter , r * http.Request , d * common.Data ) (int , error ) {
0 commit comments