forked from root-gg/plik
/
removeFile.go
114 lines (94 loc) · 3.49 KB
/
removeFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
Plik upload server
The MIT License (MIT)
Copyright (c) <2015>
- Mathieu Bodjikian <mathieu@bodjikian.fr>
- Charles-Antoine Mathieu <skatkatt@root.gg>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
package handlers
import (
"fmt"
"net/http"
"github.com/root-gg/juliet"
"github.com/root-gg/plik/server/common"
"github.com/root-gg/plik/server/dataBackend"
"github.com/root-gg/plik/server/metadataBackend"
"github.com/root-gg/utils"
)
// RemoveFile remove a file from an existing upload
func RemoveFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
log := common.GetLogger(ctx)
// Get upload from context
upload := common.GetUpload(ctx)
if upload == nil {
// This should never append
log.Critical("Missing upload in removeFileHandler")
common.Fail(ctx, req, resp, "Internal error", 500)
return
}
// Check authorization
if !upload.Removable && !upload.IsAdmin {
log.Warningf("Unable to remove file : unauthorized")
common.Fail(ctx, req, resp, "You are not allowed to remove file from this upload", 403)
return
}
// Get file from context
file := common.GetFile(ctx)
if file == nil {
// This should never append
log.Critical("Missing file in removeFileHandler")
common.Fail(ctx, req, resp, "Internal error", 500)
return
}
// Check if file is not already removed
if file.Status == "removed" {
log.Warning("Can't remove an already removed file")
common.Fail(ctx, req, resp, fmt.Sprintf("File %s has already been removed", file.Name), 404)
return
}
// Set status to removed, and save metadatas
file.Status = "removed"
if err := metadataBackend.GetMetaDataBackend().AddOrUpdateFile(ctx, upload, file); err != nil {
log.Warningf("Unable to update metadata : %s", err)
common.Fail(ctx, req, resp, "Unable to update upload metadata", 500)
return
}
// Remove file from data backend
// Get file in data backend
var backend dataBackend.DataBackend
if upload.Stream {
backend = dataBackend.GetStreamBackend()
} else {
backend = dataBackend.GetDataBackend()
}
if err := backend.RemoveFile(ctx, upload, file.ID); err != nil {
log.Warningf("Unable to delete file : %s", err)
common.Fail(ctx, req, resp, "Unable to delete file", 500)
return
}
// Remove upload if no files anymore
RemoveUploadIfNoFileAvailable(ctx, upload)
// Print upload metadata in the json response.
json, err := utils.ToJson(upload)
if err != nil {
log.Warningf("Unable to serialize json response : %s", err)
common.Fail(ctx, req, resp, "Unable to serialize json response", 500)
return
}
resp.Write(json)
}