This commit is contained in:
2022-05-17 01:56:44 +03:00
commit 3a9b8c8468
117 changed files with 7175 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
module fileserver
go 1.17
require github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e
+2
View File
@@ -0,0 +1,2 @@
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e h1:ogUKYFNcdYUIBSLibE4+EjbTJazoHr5JsWWx21Lpn8c=
github.com/aandryashin/matchers v0.0.0-20161126170413-435295ea180e/go.mod h1:cbmYNkm9xeQlNoWEPtOUcvNok2gSD7ErMnYkRW+eHi8=
+84
View File
@@ -0,0 +1,84 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
)
func main() {
dir, err := downloadsDir()
if err != nil {
log.Fatal(err)
}
log.Fatal(http.ListenAndServe(":8080", mux(dir)))
}
func downloadsDir() (string, error) {
homeDir := os.Getenv("HOME")
if homeDir == "" {
homeDir = "/home/selenium"
}
dir := filepath.Join(homeDir, "Downloads")
err := os.MkdirAll(dir, 0755)
if err != nil {
return "", fmt.Errorf("failed to create downloads dir: %v", err)
}
return dir, nil
}
const jsonParam = "json"
func mux(dir string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodDelete {
deleteFileIfExists(w, r, dir)
return
}
if _, ok := r.URL.Query()[jsonParam]; ok {
listFilesAsJson(w, dir)
return
}
http.FileServer(http.Dir(dir)).ServeHTTP(w, r)
})
return mux
}
func listFilesAsJson(w http.ResponseWriter, dir string) {
files, err := ioutil.ReadDir(dir)
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().After(files[j].ModTime())
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ret := []string{}
for _, f := range files {
ret = append(ret, f.Name())
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(ret)
}
func deleteFileIfExists(w http.ResponseWriter, r *http.Request, dir string) {
fileName := strings.TrimPrefix(r.URL.Path, "/")
filePath := filepath.Join(dir, fileName)
_, err := os.Stat(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Unknown file %s", fileName), http.StatusNotFound)
return
}
err = os.Remove(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to delete file %s: %v", fileName, err), http.StatusInternalServerError)
return
}
}
+58
View File
@@ -0,0 +1,58 @@
package main
import (
. "github.com/aandryashin/matchers"
. "github.com/aandryashin/matchers/httpresp"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
var (
dir string
srv *httptest.Server
)
func init() {
dir, _ = ioutil.TempDir("", "fileserver")
srv = httptest.NewServer(mux(dir))
}
func withUrl(path string) string {
return srv.URL + path
}
func TestDownloadAndRemoveFile(t *testing.T) {
tempFile, _ := ioutil.TempFile(dir, "fileserver")
ioutil.WriteFile(tempFile.Name(), []byte("test-data"), 0644)
tempFileName := filepath.Base(tempFile.Name())
resp, err := http.Get(withUrl("/" + tempFileName))
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{200})
_, err = os.Stat(tempFile.Name())
AssertThat(t, err, Is{nil})
rsp, err := http.Get(withUrl("/?json"))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusOK})
var files []string
AssertThat(t, rsp, IsJson{&files})
AssertThat(t, files, EqualTo{[]string{tempFileName}})
req, _ := http.NewRequest(http.MethodDelete, withUrl("/" + tempFileName), nil)
resp, err = http.DefaultClient.Do(req)
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{200})
_, err = os.Stat(tempFile.Name())
AssertThat(t, err, Not{nil})
}
func TestRemoveMissingFile(t *testing.T) {
req, _ := http.NewRequest(http.MethodDelete, withUrl("/missing-file"), nil)
resp, err := http.DefaultClient.Do(req)
AssertThat(t, err, Is{nil})
AssertThat(t, resp, Code{404})
}