-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathfile.go
More file actions
255 lines (212 loc) · 6 KB
/
file.go
File metadata and controls
255 lines (212 loc) · 6 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cmd
import (
"fmt"
"os"
"github.com/root-gg/utils"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/root-gg/plik/server/common"
"github.com/root-gg/plik/server/server"
)
type fileFlagParams struct {
uploadID string
fileID string
human bool
all bool
}
var fileParams = fileFlagParams{}
// fileCmd represents all file command
var fileCmd = &cobra.Command{
Use: "file",
Short: "Manipulate files",
}
// listFilesCmd represents the "file list" command
var listFilesCmd = &cobra.Command{
Use: "list",
Short: "List files",
Example: ` plikd file list
plikd file list --upload abc123
plikd file list --file def456
plikd file list --human=false`,
Run: listFiles,
}
// showFileCmd represents the "file show" command
var showFileCmd = &cobra.Command{
Use: "show",
Short: "Show file info",
Example: ` plikd file show --file abc123`,
Run: showFile,
}
// deleteFileCmd represents the "file delete" command
var deleteFileCmd = &cobra.Command{
Use: "delete",
Short: "Delete a file, an upload, or all uploads",
Long: `Delete a file, an upload, or all uploads.
You must specify exactly one of --file, --upload, or --all.`,
Example: ` plikd file delete --file abc123
plikd file delete --upload def456
plikd file delete --all`,
Run: deleteFiles,
}
func init() {
rootCmd.AddCommand(fileCmd)
// Here you will define your flags and configuration settings.
fileCmd.PersistentFlags().StringVar(&fileParams.uploadID, "upload", "", "upload ID")
fileCmd.PersistentFlags().StringVar(&fileParams.fileID, "file", "", "file ID")
fileCmd.AddCommand(listFilesCmd)
listFilesCmd.Flags().BoolVar(&fileParams.human, "human", true, "human readable size")
fileCmd.AddCommand(showFileCmd)
fileCmd.AddCommand(deleteFileCmd)
deleteFileCmd.Flags().BoolVar(&fileParams.all, "all", false, "delete ALL uploads (requires confirmation)")
}
func listFiles(cmd *cobra.Command, args []string) {
initializeMetadataBackend()
display := func(file *common.File) (err error) {
var size string
if fileParams.human {
size = humanize.Bytes(uint64(file.Size))
} else {
size = fmt.Sprintf("%d", file.Size)
}
fmt.Printf("%s %s %s %s %s %s\n", file.UploadID, file.ID, size, file.Status, file.Type, file.Name)
return nil
}
if fileParams.fileID != "" {
file, err := metadataBackend.GetFile(fileParams.fileID)
if err != nil {
fmt.Printf("Unable to get file : %s\n", err)
os.Exit(1)
}
if file == nil {
fmt.Printf("File %s not found\n", fileParams.fileID)
os.Exit(1)
}
_ = display(file)
os.Exit(0)
}
if fileParams.uploadID != "" {
err := metadataBackend.ForEachUploadFiles(fileParams.uploadID, display)
if err != nil {
fmt.Printf("Unable to get upload files : %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
err := metadataBackend.ForEachFile(display)
if err != nil {
fmt.Printf("Unable to get files : %s\n", err)
os.Exit(1)
}
}
func showFile(cmd *cobra.Command, args []string) {
initializeMetadataBackend()
if fileParams.fileID == "" {
fmt.Println("Missing file id")
os.Exit(1)
}
file, err := metadataBackend.GetFile(fileParams.fileID)
if err != nil {
fmt.Printf("Unable to get file : %s\n", err)
os.Exit(1)
}
if file == nil {
fmt.Printf("File %s not found\n", fileParams.fileID)
os.Exit(1)
}
utils.Dump(file)
fmt.Printf("Upload URL : %s/#/?id=%s\n", config.GetServerURL(), file.UploadID)
fmt.Printf("File URL : %s/file/%s/%s/%s\n", config.GetServerURL(), file.UploadID, file.ID, file.Name)
}
func deleteFiles(cmd *cobra.Command, args []string) {
// Require exactly one of --file, --upload, or --all
flagCount := 0
if fileParams.fileID != "" {
flagCount++
}
if fileParams.uploadID != "" {
flagCount++
}
if fileParams.all {
flagCount++
}
if flagCount == 0 {
fmt.Println("Please specify one of --file, --upload, or --all")
_ = cmd.Usage()
os.Exit(1)
}
if flagCount > 1 {
fmt.Println("Please specify only one of --file, --upload, or --all")
os.Exit(1)
}
initializeMetadataBackend()
if fileParams.fileID != "" {
file, err := metadataBackend.GetFile(fileParams.fileID)
if err != nil {
fmt.Printf("Unable to get file : %s\n", err)
os.Exit(1)
}
if file == nil {
fmt.Printf("File %s not found\n", fileParams.fileID)
os.Exit(1)
}
// Ask confirmation
fmt.Printf("Do you really want to remove this file %s %s ? [y/N]\n", file.ID, file.Name)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}
err = metadataBackend.RemoveFile(file)
if err != nil {
fmt.Printf("Unable to remove file %s : %s\n", fileParams.fileID, err)
os.Exit(1)
}
fmt.Printf("File %s has been removed\n", fileParams.fileID)
} else if fileParams.uploadID != "" {
// Ask confirmation
fmt.Printf("Do you really want to remove this upload %s ? [y/N]\n", fileParams.uploadID)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}
err = metadataBackend.RemoveUpload(fileParams.uploadID)
if err != nil {
fmt.Printf("Unable to remove upload %s : %s\n", fileParams.uploadID, err)
os.Exit(1)
}
fmt.Printf("Upload %s has been removed\n", fileParams.uploadID)
} else if fileParams.all {
// Ask confirmation
fmt.Printf("Do you really want to remove ALL uploads ? [y/N]\n")
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}
deleteUpload := func(upload *common.Upload) error {
return metadataBackend.RemoveUpload(upload.ID)
}
err = metadataBackend.ForEachUpload(deleteUpload)
if err != nil {
fmt.Printf("Unable to delete uploads : %s\n", err)
os.Exit(1)
}
fmt.Println("All uploads have been removed")
}
// Clean data backend
plik := server.NewPlikServer(config)
plik.WithMetadataBackend(metadataBackend)
initializeDataBackend()
plik.WithDataBackend(dataBackend)
plik.Clean()
}