File overwriting support. Fixes #8
This commit is contained in:
parent
8ad079ed0a
commit
e9132a1193
@ -645,6 +645,49 @@ func TestPutAndDelete(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutAndOverwrite(t *testing.T) {
|
||||
var myjson RespOkJSON
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
goji.DefaultMux.ServeHTTP(w, req)
|
||||
|
||||
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Overwrite it
|
||||
w = httptest.NewRecorder()
|
||||
req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
|
||||
req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
|
||||
goji.DefaultMux.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
||||
}
|
||||
|
||||
// Make sure it's the new file
|
||||
w = httptest.NewRecorder()
|
||||
req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
|
||||
goji.DefaultMux.ServeHTTP(w, req)
|
||||
|
||||
if w.Code == 404 {
|
||||
t.Fatal("Status code was 404")
|
||||
}
|
||||
|
||||
if w.Body.String() != "New file content" {
|
||||
t.Fatal("File did not contain 'New file content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutAndSpecificDelete(t *testing.T) {
|
||||
var myjson RespOkJSON
|
||||
|
||||
|
10
upload.go
10
upload.go
@ -220,6 +220,16 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
||||
_, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
|
||||
|
||||
fileexists := err == nil
|
||||
// Check if the delete key matches, in which case overwrite
|
||||
if fileexists {
|
||||
metad, merr := metadataRead(upload.Filename)
|
||||
if merr == nil {
|
||||
if upReq.deletionKey == metad.DeleteKey {
|
||||
fileexists = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for fileexists {
|
||||
counter, err := strconv.Atoi(string(barename[len(barename)-1]))
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user