|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"strings" |
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation" |
|
|
|
"github.com/chrislusf/seaweedfs/weed/security" |
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats" |
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage" |
|
|
|
) |
|
|
@ -40,13 +41,24 @@ func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volume |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Takes one volumeId only, can not do batch lookup
|
|
|
|
// If "fileId" is provided, this returns the fileId location and a JWT to update or delete the file.
|
|
|
|
// If "volumeId" is provided, this only returns the volumeId location
|
|
|
|
func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
vid := r.FormValue("volumeId") |
|
|
|
if vid != "" { |
|
|
|
// backward compatible
|
|
|
|
commaSep := strings.Index(vid, ",") |
|
|
|
if commaSep > 0 { |
|
|
|
vid = vid[0:commaSep] |
|
|
|
} |
|
|
|
} |
|
|
|
fileId := r.FormValue("fileId") |
|
|
|
if fileId != "" { |
|
|
|
commaSep := strings.Index(fileId, ",") |
|
|
|
if commaSep > 0 { |
|
|
|
vid = fileId[0:commaSep] |
|
|
|
} |
|
|
|
} |
|
|
|
vids := []string{vid} |
|
|
|
collection := r.FormValue("collection") //optional, but can be faster if too many collections
|
|
|
|
volumeLocations := ms.lookupVolumeId(vids, collection) |
|
|
@ -54,6 +66,8 @@ func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) |
|
|
|
httpStatus := http.StatusOK |
|
|
|
if location.Error != "" { |
|
|
|
httpStatus = http.StatusNotFound |
|
|
|
} else { |
|
|
|
ms.maybeAddJwtAuthorization(w, fileId) |
|
|
|
} |
|
|
|
writeJsonQuiet(w, r, httpStatus, location) |
|
|
|
} |
|
|
@ -88,8 +102,17 @@ func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) |
|
|
|
} |
|
|
|
fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option) |
|
|
|
if err == nil { |
|
|
|
ms.maybeAddJwtAuthorization(w, fid) |
|
|
|
writeJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count}) |
|
|
|
} else { |
|
|
|
writeJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (ms *MasterServer) maybeAddJwtAuthorization(w http.ResponseWriter, fileId string) { |
|
|
|
encodedJwt := security.GenJwt(ms.guard.SigningKey, fileId) |
|
|
|
if encodedJwt == "" { |
|
|
|
return |
|
|
|
} |
|
|
|
w.Header().Set("Authorization", "BEARER "+string(encodedJwt)) |
|
|
|
} |