@@ -11,6 +11,7 @@ import (
11
11
"io/ioutil"
12
12
"net/http"
13
13
"strconv"
14
+ "strings"
14
15
"time"
15
16
16
17
"github.com/spf13/afero"
@@ -198,8 +199,50 @@ func shareLinkGetHandler(w http.ResponseWriter, r *http.Request, d *common.Data)
198
199
return http .StatusInternalServerError , err
199
200
}
200
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
+ }
201
210
w .Header ().Set ("Content-Type" , "application/json" )
202
- 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 )
203
246
}
204
247
205
248
type ShareLinkPostRequestBody struct {
@@ -276,11 +319,12 @@ func shareLinkPostHandler(w http.ResponseWriter, r *http.Request, d *common.Data
276
319
277
320
// Calculate expire time
278
321
expireTime := time .Now ().Add (time .Duration (requestBody .ExpireIn ) * time .Millisecond )
279
-
322
+ pathMD5 := common . Md5String ( r . URL . Path + fmt . Sprint ( time . Now (). UnixNano ()))
280
323
newShareLink := postgres.ShareLink {
281
- LinkURL : host + "/share_link/" + common . Md5String ( r . URL . Path + fmt . Sprint ( time . Now (). UnixNano ())) ,
324
+ LinkURL : host + "/share_link/" + pathMD5 ,
282
325
PathID : pathID ,
283
326
Path : r .URL .Path ,
327
+ PathMD5 : pathMD5 ,
284
328
Password : common .Md5String (requestBody .Password ),
285
329
OwnerID : ownerID ,
286
330
OwnerName : ownerName ,
0 commit comments