fix archive files list
This commit is contained in:
@@ -20,7 +20,6 @@ type DirectoryEntry struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
SHASum string `json:"shaSum"`
|
|
||||||
ModTime time.Time `json:"modTime"`
|
ModTime time.Time `json:"modTime"`
|
||||||
IsVideo bool `json:"isVideo"`
|
IsVideo bool `json:"isVideo"`
|
||||||
IsDirectory bool `json:"isDirectory"`
|
IsDirectory bool `json:"isDirectory"`
|
||||||
@@ -32,17 +31,9 @@ func walkDir(root string) (*[]DirectoryEntry, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var validEntries int
|
var files []DirectoryEntry
|
||||||
|
|
||||||
for _, d := range dirs {
|
for _, d := range dirs {
|
||||||
if utils.IsValidEntry(d) {
|
|
||||||
validEntries++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
files := make([]DirectoryEntry, validEntries+1)
|
|
||||||
|
|
||||||
for i, d := range dirs {
|
|
||||||
if !utils.IsValidEntry(d) {
|
if !utils.IsValidEntry(d) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -54,15 +45,14 @@ func walkDir(root string) (*[]DirectoryEntry, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
files[i] = DirectoryEntry{
|
files = append(files, DirectoryEntry{
|
||||||
Path: path,
|
Path: path,
|
||||||
Name: d.Name(),
|
Name: d.Name(),
|
||||||
Size: info.Size(),
|
Size: info.Size(),
|
||||||
SHASum: utils.ShaSumString(path),
|
|
||||||
IsVideo: utils.IsVideo(d),
|
IsVideo: utils.IsVideo(d),
|
||||||
IsDirectory: d.IsDir(),
|
IsDirectory: d.IsDir(),
|
||||||
ModTime: info.ModTime(),
|
ModTime: info.ModTime(),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return &files, err
|
return &files, err
|
||||||
@@ -77,8 +67,7 @@ func ListDownloaded(w http.ResponseWriter, r *http.Request) {
|
|||||||
root := config.Instance().DownloadPath
|
root := config.Instance().DownloadPath
|
||||||
req := new(ListRequest)
|
req := new(ListRequest)
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -96,9 +85,8 @@ func ListDownloaded(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = json.NewEncoder(w).Encode(files)
|
|
||||||
|
|
||||||
if err != nil {
|
if err := json.NewEncoder(w).Encode(files); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,21 +96,13 @@ type DeleteRequest = DirectoryEntry
|
|||||||
func DeleteFile(w http.ResponseWriter, r *http.Request) {
|
func DeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||||
req := new(DeleteRequest)
|
req := new(DeleteRequest)
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := utils.ShaSumString(req.Path)
|
if err := os.Remove(req.Path); err != nil {
|
||||||
if sum != req.SHASum {
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
http.Error(w, "shasum mismatch", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.Remove(req.Path)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "shasum mismatch", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +142,7 @@ func SendFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
http.ServeFile(w, r, filename)
|
http.ServeFile(w, r, filename)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
|||||||
Reference in New Issue
Block a user