Skip to content

Commit eb31989

Browse files
committed
use share link
1 parent 126ec1f commit eb31989

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

pkg/http/http.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"files/pkg/fileutils"
55
"files/pkg/preview"
66
"files/pkg/rpc"
7+
"net/http"
8+
79
"github.com/gin-gonic/gin"
810
"k8s.io/klog/v2"
9-
"net/http"
1011

1112
"github.com/gorilla/mux"
1213

@@ -40,6 +41,9 @@ func NewHandler(
4041

4142
r.HandleFunc("/health", healthHandler)
4243

44+
share_link := r.PathPrefix("/share_link").Subrouter()
45+
share_link.Handle("/{[a-fA-F0-9]{32}}", monkey(useShareLinkGetHandler, "/share_link")).Methods("GET")
46+
4347
api := r.PathPrefix("/api").Subrouter()
4448

4549
api.PathPrefix("/resources").Handler(monkey(resourceGetHandler, "/api/resources")).Methods("GET")

pkg/http/share.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io/ioutil"
1212
"net/http"
1313
"strconv"
14+
"strings"
1415
"time"
1516

1617
"github.com/spf13/afero"
@@ -198,8 +199,50 @@ func shareLinkGetHandler(w http.ResponseWriter, r *http.Request, d *common.Data)
198199
return http.StatusInternalServerError, err
199200
}
200201

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+
}
201210
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)
203246
}
204247

205248
type ShareLinkPostRequestBody struct {
@@ -276,11 +319,12 @@ func shareLinkPostHandler(w http.ResponseWriter, r *http.Request, d *common.Data
276319

277320
// Calculate expire time
278321
expireTime := time.Now().Add(time.Duration(requestBody.ExpireIn) * time.Millisecond)
279-
322+
pathMD5 := common.Md5String(r.URL.Path + fmt.Sprint(time.Now().UnixNano()))
280323
newShareLink := postgres.ShareLink{
281-
LinkURL: host + "/share_link/" + common.Md5String(r.URL.Path+fmt.Sprint(time.Now().UnixNano())),
324+
LinkURL: host + "/share_link/" + pathMD5,
282325
PathID: pathID,
283326
Path: r.URL.Path,
327+
PathMD5: pathMD5,
284328
Password: common.Md5String(requestBody.Password),
285329
OwnerID: ownerID,
286330
OwnerName: ownerName,

pkg/postgres/share_link.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package postgres
22

33
import (
4-
"k8s.io/klog/v2"
54
"time"
5+
6+
"k8s.io/klog/v2"
67
)
78

89
var STATUS_INACTIVE = 0
@@ -25,7 +26,8 @@ type ShareLink struct {
2526
LinkURL string `gorm:"type:text;not null;index:idx_share_links_link_url"`
2627
PathID uint64 `gorm:"type:bigint;not null;index:idx_share_links_path_id"`
2728
Path string `gorm:"type:text;not null;index:idx_share_links_path"`
28-
Password string `gorm:"type:varchar(32);not null"`
29+
PathMD5 string `gorm:"type:varchar(32);not null;index:idx_share_links_path_md5_password"`
30+
Password string `gorm:"type:varchar(32);not null;index:idx_share_links_path_md5_password"`
2931
OwnerID string `gorm:"type:text;not null"`
3032
OwnerName string `gorm:"type:text;not null"`
3133
Permission int `gorm:"not null"`

0 commit comments

Comments
 (0)