Use hash as file name to de-duplicate uploads
parent
222afd3957
commit
1556bd5128
|
@ -1,9 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -34,7 +36,15 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = w.Write([]byte("Failed to get file"))
|
_, _ = w.Write([]byte("Failed to get file"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileName := generateRandomString(time.Now(), 15)
|
hashFile, _, _ := r.FormFile("file")
|
||||||
|
h := sha256.New()
|
||||||
|
defer func() { _ = hashFile.Close() }()
|
||||||
|
if _, err := io.Copy(h, hashFile); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = w.Write([]byte("Failed to calculate hash of file"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fileName := fmt.Sprintf("%x", h.Sum(nil))
|
||||||
mimeType := header.Header.Get("content-type")
|
mimeType := header.Header.Get("content-type")
|
||||||
originalName := header.Filename
|
originalName := header.Filename
|
||||||
if strings.Contains(mimeType, "jpeg") || strings.Contains(originalName, ".jpeg") || strings.Contains(originalName, ".jpg") {
|
if strings.Contains(mimeType, "jpeg") || strings.Contains(originalName, ".jpeg") || strings.Contains(originalName, ".jpg") {
|
||||||
|
|
Loading…
Reference in New Issue