Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
added filetype checker
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Mar 3, 2024
1 parent c6efe02 commit e286c84
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/peterbourgon/ff"
)

const version = "2.3.0"
const version = "2.4.0"

var (
dataDir = flag.String("d", "./data", "Folder to store files")
Expand Down Expand Up @@ -291,6 +291,9 @@ func handleStore(w http.ResponseWriter, r *http.Request) {
return
}

// Get the filetype based on magic number
contentType := http.DetectContentType(buf.Bytes())

newFile, err := os.Create(filename)
if err != nil {
http.Error(w, "Failed to create file", http.StatusInternalServerError)
Expand All @@ -305,8 +308,8 @@ func handleStore(w http.ResponseWriter, r *http.Request) {
}

// Write the hashes and the current Unix time to the "data" table in the database
_, err = db.Exec(`INSERT INTO data (id, sha256, sha1, md5, crc32, uploaded) VALUES (?, ?, ?, ?, ?, ?)`,
hashes["sha256"], hashes["sha256"], hashes["sha1"], hashes["md5"], hashes["crc32"], time.Now().Unix())
_, err = db.Exec(`INSERT INTO data (id, sha256, sha1, md5, crc32, uploaded, type) VALUES (?, ?, ?, ?, ?, ?, ?)`,
hashes["sha256"], hashes["sha256"], hashes["sha1"], hashes["md5"], hashes["crc32"], time.Now().Unix(), contentType)
if err != nil {
http.Error(w, "Failed to store hashes in database", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -587,7 +590,7 @@ func handleShorten(w http.ResponseWriter, r *http.Request) {
return
}

if isValidURL(request.URL) == false {
if !isValidURL(request.URL) {
response.Success = false
response.Error = "Url is not valid"
response.ID = ""
Expand Down

0 comments on commit e286c84

Please sign in to comment.