2012-09-26 18:27:10 +08:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2014-04-16 00:09:40 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-12-15 14:38:58 +08:00
|
|
|
"fmt"
|
2014-04-16 00:09:40 +08:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2015-12-03 16:27:02 +08:00
|
|
|
"net/http"
|
|
|
|
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/security"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2012-09-26 18:27:10 +08:00
|
|
|
)
|
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
type DeleteResult struct {
|
2015-12-03 16:27:02 +08:00
|
|
|
Fid string `json:"fid"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
|
|
|
|
2015-02-08 07:35:28 +08:00
|
|
|
func DeleteFile(master string, fileId string, jwt security.EncodedJwt) error {
|
2014-04-14 16:07:11 +08:00
|
|
|
fileUrl, err := LookupFileId(master, fileId)
|
2013-11-19 15:03:59 +08:00
|
|
|
if err != nil {
|
2015-12-15 14:38:58 +08:00
|
|
|
return fmt.Errorf("Failed to lookup %s:%v", fileId, err)
|
2013-08-14 15:31:02 +08:00
|
|
|
}
|
2015-12-15 14:38:58 +08:00
|
|
|
err = util.Delete(fileUrl, jwt)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to delete %s:%v", fileUrl, err)
|
|
|
|
}
|
|
|
|
return nil
|
2012-09-26 18:27:10 +08:00
|
|
|
}
|
2014-04-16 00:09:40 +08:00
|
|
|
|
|
|
|
func ParseFileId(fid string) (vid string, key_cookie string, err error) {
|
|
|
|
commaIndex := strings.Index(fid, ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
return "", "", errors.New("Wrong fid format.")
|
|
|
|
}
|
|
|
|
return fid[:commaIndex], fid[commaIndex+1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteFilesResult struct {
|
|
|
|
Errors []string
|
|
|
|
Results []DeleteResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteFiles(master string, fileIds []string) (*DeleteFilesResult, error) {
|
|
|
|
vid_to_fileIds := make(map[string][]string)
|
|
|
|
ret := &DeleteFilesResult{}
|
|
|
|
var vids []string
|
|
|
|
for _, fileId := range fileIds {
|
|
|
|
vid, _, err := ParseFileId(fileId)
|
|
|
|
if err != nil {
|
2015-12-03 16:27:02 +08:00
|
|
|
ret.Results = append(ret.Results, DeleteResult{
|
|
|
|
Fid: vid,
|
|
|
|
Status: http.StatusBadRequest,
|
|
|
|
Error: err.Error()},
|
|
|
|
)
|
2014-04-16 00:09:40 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := vid_to_fileIds[vid]; !ok {
|
|
|
|
vid_to_fileIds[vid] = make([]string, 0)
|
|
|
|
vids = append(vids, vid)
|
|
|
|
}
|
|
|
|
vid_to_fileIds[vid] = append(vid_to_fileIds[vid], fileId)
|
|
|
|
}
|
|
|
|
|
|
|
|
lookupResults, err := LookupVolumeIds(master, vids)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server_to_fileIds := make(map[string][]string)
|
|
|
|
for vid, result := range lookupResults {
|
|
|
|
if result.Error != "" {
|
|
|
|
ret.Errors = append(ret.Errors, result.Error)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, location := range result.Locations {
|
2015-02-03 02:16:50 +08:00
|
|
|
if _, ok := server_to_fileIds[location.Url]; !ok {
|
|
|
|
server_to_fileIds[location.Url] = make([]string, 0)
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
2015-02-03 02:16:50 +08:00
|
|
|
server_to_fileIds[location.Url] = append(
|
|
|
|
server_to_fileIds[location.Url], vid_to_fileIds[vid]...)
|
2014-04-16 00:09:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2015-12-03 16:27:02 +08:00
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
for server, fidList := range server_to_fileIds {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(server string, fidList []string) {
|
|
|
|
defer wg.Done()
|
|
|
|
values := make(url.Values)
|
|
|
|
for _, fid := range fidList {
|
|
|
|
values.Add("fid", fid)
|
|
|
|
}
|
|
|
|
jsonBlob, err := util.Post("http://"+server+"/delete", values)
|
|
|
|
if err != nil {
|
2014-04-16 00:20:28 +08:00
|
|
|
ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
|
2014-04-16 00:09:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var result []DeleteResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &result)
|
|
|
|
if err != nil {
|
2014-04-16 00:20:28 +08:00
|
|
|
ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
|
2014-04-16 00:09:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ret.Results = append(ret.Results, result...)
|
|
|
|
}(server, fidList)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|