-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
483 lines (407 loc) · 11.7 KB
/
main.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
)
type FileHash struct {
FileName string `json:"fileName"`
Hash string `json:"hash"`
}
var fileHashes []FileHash
var fileHashesMutex sync.Mutex
var fileStoreMutex sync.Mutex
const uploadDirectory = "./uploads"
const filestoreMetadata = "./metadata" // Optimization with hashing technique.
const jsonFilePath = "./metadata/fileHashes.json"
func init() {
// Create the upload directory if it doesn't exist
err := os.MkdirAll(uploadDirectory, 0755)
if err != nil {
fmt.Println("Error creating upload directory:", err)
return
}
// Create metadata directory if it doesn't exist
err = os.MkdirAll(filestoreMetadata, 0755)
if err != nil {
fmt.Println("Error creating metadata directory:", err)
return
}
// Create json to store hash or Load eixtsing filehash
loadFileHashes()
}
func main() {
// 1. Handle file uploads
http.HandleFunc("/upload", uploadHandler)
// 1a. FileHashMatching
http.HandleFunc("/findMatchingFile", findMatchingFileHandler)
// 1a. CopyFile
http.HandleFunc("/copyFile", copyFileHandler)
// 2. Handle file listing
http.HandleFunc("/list", listHandler)
// 3. Handle file deletes
http.HandleFunc("/delete/", deleteHandler)
// 4. Handle file updates
http.HandleFunc("/update", updateHandler)
// 5. Count words in files
http.HandleFunc("/wordCount", wordCountHandler)
// Start the server
port := 8080
fmt.Printf("Server is running on http://127.0.0.1:%d\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
file, handler, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error retrieving file from form", http.StatusBadRequest)
return
}
defer file.Close()
filePath := fmt.Sprintf("%s/%s", uploadDirectory, handler.Filename)
if fileExists(filePath) {
http.Error(w, "Error Adding file to server", http.StatusConflict)
return
}
dst, err := os.Create(filePath)
if err != nil {
http.Error(w, "Error creating file on server", http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, "Error copying file to server", http.StatusInternalServerError)
return
}
// Store the hash value in JSON file
hash := r.FormValue("hash")
// Write the metadata (filename and hash) to the file
metadatainfo := FileHash{FileName: handler.Filename, Hash: hash}
err = appendDataToFile(metadatainfo, jsonFilePath)
if err != nil {
http.Error(w, "Unable to write metadata to file", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "File '%s' uploaded successfully.", handler.Filename)
}
func updateHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Traversing through update function.")
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
file, handler, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error retrieving file from form", http.StatusBadRequest)
return
}
defer file.Close()
filePath := fmt.Sprintf("%s/%s", uploadDirectory, handler.Filename)
fmt.Println(filePath)
dst, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
http.Error(w, "Error creating file on server", http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, "Error copying file to server", http.StatusInternalServerError)
return
}
// Store the hash value in JSON file
hash := r.FormValue("hash")
//remove existing hash entry for the updated file.
loadFileHashes()
for i, fh := range fileHashes {
if fh.FileName == handler.Filename {
fmt.Println(fh.FileName)
fileHashes = append(fileHashes[:i], fileHashes[i+1:]...)
err := writeJSONToFile(fileHashes, jsonFilePath)
if err != nil {
http.Error(w, "Error removing hash.", http.StatusInternalServerError)
return
}
}
}
// Write the metadata (filename and hash) to the file
metadatainfo := FileHash{FileName: handler.Filename, Hash: hash}
err = appendDataToFile(metadatainfo, jsonFilePath)
if err != nil {
http.Error(w, "Unable to write metadata to file", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "File '%s' updated successfully.", handler.Filename)
}
func listHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// Get the list of files in the upload directory
files, err := listFiles(uploadDirectory)
if err != nil {
http.Error(w, "Error listing files", http.StatusInternalServerError)
return
}
// Respond with the list of files
w.Header().Set("Content-Type", "text/plain")
for _, file := range files {
fmt.Fprintln(w, file)
}
}
func listFiles(directory string) ([]string, error) {
var files []string
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
// Exclude directories from the list
relativePath, err := filepath.Rel(uploadDirectory, path)
if err != nil {
return err
}
files = append(files, relativePath)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// Extract the filename from the URL
fileName := r.URL.Path[len("/delete/"):]
// Remove the file from the server
filePath := filepath.Join(uploadDirectory, fileName)
err := os.Remove(filePath)
if err != nil {
http.Error(w, "Error deleting file", http.StatusInternalServerError)
return
}
loadFileHashes()
//remove hash entry for the deleted file.
for i, fh := range fileHashes {
if fh.FileName == fileName {
fileHashes = append(fileHashes[:i], fileHashes[i+1:]...)
err := writeJSONToFile(fileHashes, jsonFilePath)
if err != nil {
http.Error(w, "Error removing hash.", http.StatusInternalServerError)
return
}
}
}
// Respond with a success message
fmt.Fprintf(w, "File '%s' deleted successfully.", fileName)
}
func writeJSONToFile(data interface{}, filePath string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(data)
if err != nil {
return err
}
fmt.Println("Data written to", filePath)
return nil
}
func appendDataToFile(newData FileHash, filePath string) error {
// Read existing data from the JSON file
existingData, err := readExistingData(filePath)
if err != nil {
return err
}
// Append the new data
existingData = append(existingData, newData)
// Write the updated data back to the JSON file
err = writeJSONToFile(existingData, filePath)
if err != nil {
return err
}
return nil
}
func readExistingData(filePath string) ([]FileHash, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var existingData []FileHash
decoder := json.NewDecoder(file)
err = decoder.Decode(&existingData)
if err != nil && err != io.EOF {
return nil, err
}
return existingData, nil
}
func loadFileHashes() {
fileHashesMutex.Lock()
defer fileHashesMutex.Unlock()
// Append the metadata to a file
file, err := os.OpenFile(jsonFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening JSON file:", err)
return
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
fmt.Println("Error getting file info:", err)
return
}
if !(fileInfo.Size() == 0) {
data, err := os.ReadFile(jsonFilePath)
if err != nil {
fmt.Println("Error reading JSON file:", err)
return
}
err = json.Unmarshal(data, &fileHashes)
if err != nil {
fmt.Println("Error unmarshalling file hashes:", err)
return
}
}
}
func findMatchingFileHandler(w http.ResponseWriter, r *http.Request) {
// Parse request parameters
r.ParseForm()
hashToCompare := r.Form.Get("hash")
// Find the matching file for the given hash
matchingFileName, err := findMatchingFile(hashToCompare)
if err != nil {
http.Error(w, "Error finding matching file", http.StatusInternalServerError)
return
}
// Return the matching file name as JSON response
response := map[string]string{"matchingFileName": matchingFileName}
fmt.Printf("File with matching content identified: %s\n", matchingFileName)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func findMatchingFile(hashToCompare string) (string, error) {
loadFileHashes()
fileHashesMutex.Lock()
defer fileHashesMutex.Unlock()
// Look for a file with a matching hash in the JSON file
matchedfilename := ""
for _, fh := range fileHashes {
fmt.Println(fh.Hash)
fmt.Println(hashToCompare)
if fh.Hash == hashToCompare {
matchedfilename = fh.FileName
return matchedfilename, nil
}
}
return "unmatched", nil
}
func copyFileHandler(w http.ResponseWriter, r *http.Request) {
// Parse request parameters
r.ParseForm()
srcFileName := r.Form.Get("src")
destFileName := r.Form.Get("dest")
hashstring := r.Form.Get("hashstring")
// Call the copyFile function
err := copyFile(srcFileName, destFileName, hashstring)
if err != nil {
http.Error(w, fmt.Sprintf("Error copying file: %v", err), http.StatusInternalServerError)
return
}
// Respond with success message
w.WriteHeader(http.StatusOK)
w.Write([]byte("File saved by duplciation at server successfully"))
}
func copyFile(srcFile string, destFile string, hashstring string) error {
srcPath := uploadDirectory + "/" + string(srcFile)
destPath := uploadDirectory + "/" + string(destFile)
// Open the source file for reading
src_file, err := os.Open(srcPath)
if err != nil {
fmt.Println("Error opening JSON file:", err)
return nil
}
defer src_file.Close()
// Create or open the destination file for writing
dest_file, err := os.OpenFile(destPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer dest_file.Close()
// Write the metadata (filename and hash) to the file
metadatainfo := FileHash{FileName: destFile, Hash: hashstring}
err = appendDataToFile(metadatainfo, jsonFilePath)
if err != nil {
return err
}
// Copy the content from the source to the destination
_, copyerr := io.Copy(dest_file, src_file)
if copyerr != nil {
return err
}
fmt.Printf("File copied from %s to %s\n", srcPath, destPath)
return nil
}
func wordCountHandler(w http.ResponseWriter, r *http.Request) {
totalWords, err := countWordsInFiles()
if err != nil {
http.Error(w, "Error counting words", http.StatusInternalServerError)
return
}
response := map[string]int{"totalWords": totalWords}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func countWords(text string) int {
scanner := bufio.NewScanner(strings.NewReader(text))
scanner.Split(bufio.ScanWords)
var count int
for scanner.Scan() {
count++
}
return count
}
func countWordsInFiles() (int, error) {
fileStoreMutex.Lock()
defer fileStoreMutex.Unlock()
var totalWords int
err := filepath.Walk(uploadDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileContent, err := os.ReadFile(path)
if err != nil {
return err
}
words := countWords(string(fileContent))
totalWords += words
}
return nil
})
return totalWords, err
}