Skip to content

Commit

Permalink
Fix small logic bug and simplify function.
Browse files Browse the repository at this point in the history
Also change module names to reflect GH hosting.
  • Loading branch information
flxy0 committed Mar 21, 2022
1 parent 26154b8 commit fcc7c3a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
5 changes: 3 additions & 2 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ func ViewFiles(w http.ResponseWriter, r *http.Request) {
if formHash != "" {
http.Redirect(w, r, fmt.Sprintf("/viewfiles/%s", formHash), http.StatusSeeOther)
} else if len(userHash) == 0 || userHash == "" {
fmt.Println("HUH?!")
tmpl := template.Must(template.ParseFiles("templates/base.gohtml", "templates/viewfiles.gohtml"))

tmplErr := tmpl.Execute(w, nil)
if tmplErr != nil {
log.Fatal(tmplErr)
}
} else if len(userHash) > 20 {
fmt.Println("is long enough")
files, err := ioutil.ReadDir(fmt.Sprintf("uploads/%s/", userHash))

if err != nil {
Expand Down Expand Up @@ -117,6 +115,9 @@ func DeleteFiles(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Files succesfully deleted")
}

// This function handles the post form from the upload.gohtml
// It verifies that there's a valid hash and a file present upon submission
// If not, it will simply display a simple text message informing the user what is wrong
func Uploaded(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module sr.ht/flxy/wakeru
module github.com/flxy0/wakeru

go 1.18
2 changes: 1 addition & 1 deletion hashgen/hashgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"text/template"
"time"

"sr.ht/flxy/wakeru/helpers"
"github.com/flxy0/wakeru/helpers"
)

// This function takes care of generating new hashes, creating the directory for it,
Expand Down
42 changes: 13 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"os"
"strings"

"sr.ht/flxy/wakeru/files"
"sr.ht/flxy/wakeru/hashgen"
"sr.ht/flxy/wakeru/helpers"
"github.com/flxy0/wakeru/files"
"github.com/flxy0/wakeru/hashgen"
"github.com/flxy0/wakeru/helpers"
)

// Type for Template Files
Expand All @@ -37,10 +37,6 @@ func renderStaticFile(filepath string) http.HandlerFunc {
}
}

// This function handles the post form from the upload.gohtml
// It verifies that there's a valid hash and a file present upon submission
// If not, it will simply display a simple text message informing the user what is wrong

// This function takes care of all the content serving
// It checks whether the URL has the first 20 digits of an existing string
// and whether the file exists in the corresponding full hash named directory
Expand All @@ -51,13 +47,17 @@ func handleServeContent(w http.ResponseWriter, r *http.Request) {
var dirMatch string

for _, v := range helpers.ServeDirs {
if strings.HasPrefix(v, userHash) && len(userHash) == 20 {
if userHash == v[:20] {
dirMatch = v
break
}
// else {
// fmt.Fprintf(w, "Hash is wrong or doesn't exist")
// fmt.Println(userHash)
// }
}

// If dirMatch doesn't get a value assigned to it, the hash doesn't exist so we need to inform the user
if dirMatch == "" {
log.Fatal(w, "Hash is wrong or doesn't exist")
fmt.Fprintf(w, "Hash is wrong or doesn't exist")
return
}

filePath := fmt.Sprintf("uploads/%s/%s", dirMatch, filename)
Expand All @@ -70,23 +70,7 @@ func handleServeContent(w http.ResponseWriter, r *http.Request) {
return
}

file, fileErr := os.Open(filePath)
if fileErr != nil {
log.Fatal(fileErr)
return
}

fileStats, statErr := file.Stat()
if statErr != nil {
log.Fatal(statErr)
}

fileBuf := make([]byte, fileStats.Size())
file.Read(fileBuf)

w.Header().Add("Content-Type", mime.TypeByExtension("."+(strings.Split(filename, ".")[1])))
w.Write(fileBuf)
file.Close()
http.ServeFile(w, r, filePath)
}

func main() {
Expand Down

0 comments on commit fcc7c3a

Please sign in to comment.