Skip to content

Commit e244971

Browse files
committed
fix creating/geting share_link and add using share_link http interface
1 parent cf9e00e commit e244971

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

pkg/http/http.go

Lines changed: 5 additions & 1 deletion
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

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"files/pkg/files"
99
"files/pkg/postgres"
1010
"fmt"
11-
"github.com/spf13/afero"
12-
"gorm.io/gorm"
1311
"io/ioutil"
14-
"k8s.io/klog/v2"
1512
"net/http"
1613
"strconv"
14+
"strings"
1715
"time"
16+
17+
"github.com/spf13/afero"
18+
"gorm.io/gorm"
19+
"k8s.io/klog/v2"
1820
)
1921

2022
type ShareablePutRequestBody struct {
@@ -197,8 +199,50 @@ func shareLinkGetHandler(w http.ResponseWriter, r *http.Request, d *common.Data)
197199
return http.StatusInternalServerError, err
198200
}
199201

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+
}
200210
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)
202246
}
203247

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

276320
// Calculate expire time
277321
expireTime := time.Now().Add(time.Duration(requestBody.ExpireIn) * time.Millisecond)
278-
322+
pathMD5 := common.Md5String(r.URL.Path + fmt.Sprint(time.Now().UnixNano()))
279323
newShareLink := postgres.ShareLink{
280-
LinkURL: host + "/share_link/" + common.Md5String(r.URL.Path+fmt.Sprint(time.Now().UnixNano())),
324+
LinkURL: host + "/share_link/" + pathMD5,
281325
PathID: pathID,
282326
Path: r.URL.Path,
327+
PathMD5: pathMD5,
283328
Password: common.Md5String(requestBody.Password),
284329
OwnerID: ownerID,
285330
OwnerName: ownerName,
@@ -297,8 +342,12 @@ func shareLinkPostHandler(w http.ResponseWriter, r *http.Request, d *common.Data
297342
return http.StatusInternalServerError, result.Error
298343
}
299344

345+
w.Header().Set("Content-Type", "application/json")
300346
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)
302351
}
303352

304353
func shareLinkDeleteHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {

pkg/postgres/share_link.go

Lines changed: 4 additions & 2 deletions
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)