Skip to content

Commit

Permalink
process file location as first 12 chars of sha
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Jan 28, 2023
1 parent 2b3808e commit 7f6ff74
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
7 changes: 2 additions & 5 deletions crypto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"

Expand Down Expand Up @@ -30,13 +29,11 @@ func checkSignature(publickey, signature string, contents []byte) (bool, error)
return false, fmt.Errorf("failed to parse signature: %w", err)
}

shasum := sha256.Sum256(contents)

logrus.WithFields(logrus.Fields{
"fileShasum": hex.EncodeToString(shasum[:]),
"fileShasum": hex.EncodeToString(contents),
"pubkey": publickey,
"signature": signature,
}).Debug("checking signature")

return sig.Verify(shasum[:], pubkey), nil
return sig.Verify(contents, pubkey), nil
}
18 changes: 18 additions & 0 deletions filepath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"encoding/hex"
"path/filepath"
)

func createAndGetDirectory(shasum []byte) string {
hash := hex.EncodeToString(shasum)
dirs := make([]string, 0, 4)

for i := 0; i < 4; i += 1 {
start := i * 3
dirs = append(dirs, hash[start:start+3])
}

return filepath.Join(dirs...)
}
33 changes: 29 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -67,12 +70,13 @@ func upload(w http.ResponseWriter, r *http.Request) {
http.Error(w, "unable to read file", http.StatusInternalServerError)
return
}
shasum := sha256.Sum256(buf.Bytes())

pubkey := r.FormValue("pubkey")
signature := r.FormValue("signature")
logrus.WithFields(logrus.Fields{"pubkey": pubkey, "signature": signature}).Debug("form values")

validSig, err := checkSignature(pubkey, signature, buf.Bytes())
validSig, err := checkSignature(pubkey, signature, shasum[:])
if err != nil {
logrus.WithError(err).Error("error checking the signature")
http.Error(w, "error checking signature", http.StatusBadRequest)
Expand All @@ -83,8 +87,28 @@ func upload(w http.ResponseWriter, r *http.Request) {
return
}

// create directory path to put file
directory := createAndGetDirectory(shasum[:])
fullDirectory := filepath.Join("uploads", directory)
if err := os.MkdirAll(fullDirectory, os.ModePerm); err != nil {
logrus.WithError(err).Error("error creating the directory on filesystem")
http.Error(w, "error preparing the file", http.StatusInternalServerError)
return
}

// get name and paths to file
extension := filepath.Ext(fileHeader.Filename)
name := fmt.Sprintf("%s%s", hex.EncodeToString(shasum[:]), extension)
path := filepath.Join(directory, name)
fullPath, err := filepath.Abs(filepath.Join(fullDirectory, name))
if err != nil {
logrus.WithError(err).Error("error calculating the local path on filesystem")
http.Error(w, "error opening the file on filesystem", http.StatusInternalServerError)
return
}

// save file to the filesystem
dst, err := os.Create(filepath.Join(".", "uploads", fileHeader.Filename))
dst, err := os.Create(fullPath)
if err != nil {
logrus.WithError(err).Error("error opening the file on filesystem")
http.Error(w, "error opening the file on filesystem", http.StatusInternalServerError)
Expand All @@ -98,10 +122,11 @@ func upload(w http.ResponseWriter, r *http.Request) {
return
}

// respond with file data
url := url.URL{
Scheme: "https",
Scheme: "http",
Host: "localhost:8080",
Path: fileHeader.Filename,
Path: path,
}
res := response{
Name: fileHeader.Filename,
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
func main() {
logrus.SetLevel(logrus.DebugLevel)
// create directory to hold uploads
err := os.MkdirAll("./uploads", os.ModePerm)
if err != nil {
if err := os.MkdirAll("./uploads", os.ModePerm); err != nil {
logrus.WithError(err).Fatal("unable to create the upload directory")
return
}
Expand Down

0 comments on commit 7f6ff74

Please sign in to comment.