Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/filedownload'
Browse files Browse the repository at this point in the history
Finished file download endpoint
  • Loading branch information
pandadiestro committed Nov 20, 2024
2 parents b360805 + fa16054 commit 31f648c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 24 deletions.
9 changes: 7 additions & 2 deletions stiller-backend/cmd/serve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ func main() {
log.Fatalln("[ ❌ ] invalid router, exiting...")
}

log.Println("[ ✅ ] server started at port", stiller.StillerConfig.Port)
log.Println(
"[ ✅ ] server started at port",
stiller.StillerConfig.Addr,
)

log.Fatalln(http.ListenAndServe(stiller.StillerConfig.Port, new_router))
log.Fatalln(
http.ListenAndServe(stiller.StillerConfig.Addr, new_router),
)
}


4 changes: 2 additions & 2 deletions stiller-backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/profclems/go-dotenv"

type ConfigType struct {
// local directory where to retrieve/store data from/to
Port string
Addr string
FilesPath string
DBPath string
Secret []byte
Expand All @@ -15,7 +15,7 @@ func newConfig() *ConfigType {
dotenv.Load()

return &ConfigType{
Port: dotenv.GetString("Port"),
Addr: dotenv.GetString("Addr"),
FilesPath: dotenv.GetString("FilesPath"),
DBPath: dotenv.GetString("DBPath"),
Secret: []byte(dotenv.GetString("Secret")),
Expand Down
5 changes: 0 additions & 5 deletions stiller-backend/db/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,3 @@ values
(1, 'van gogh'),
(2, 'picasso');

insert into
user (tier, displayname, username, mail, bpasswd)
values
(0, 'admin', 'admin', '[email protected]', 'skibidi pana');

26 changes: 20 additions & 6 deletions stiller-backend/internal/handlers/auth/userlogin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

jsonexp "github.com/go-json-experiment/json"
"github.com/julienschmidt/httprouter"
"github.com/leporo/sqlf"
"golang.org/x/crypto/bcrypt"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
Expand Down Expand Up @@ -37,10 +38,20 @@ func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

user_bpwd := string("")
amount := int(0)
query := `select id, bpasswd from user where username=?1;`

query_stmt := sqlf.
Select("id, bpasswd").
From("user").
Where("username = ?", payload.Username)

query := query_stmt.String()
new_dbconn, dbconn_err := dbutils.NewConn()
if handleutils.RequestLog(dbconn_err, "", http.StatusInternalServerError, &w) {
if handleutils.RequestLog(
dbconn_err,
"",
http.StatusInternalServerError,
&w,
) {
return
}

Expand All @@ -53,9 +64,7 @@ func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
user_bpwd = stmt.GetText("bpasswd")
return nil
},
Args: []any{
payload.Username,
},
Args: query_stmt.Args(),
})

if amount == 0 {
Expand All @@ -68,7 +77,12 @@ func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log.Println("pwd:", user_bpwd)

cmp_err := bcrypt.CompareHashAndPassword([]byte(user_bpwd), []byte(payload.Pwd))
if handleutils.RequestLog(cmp_err, "", http.StatusNotFound, &w) {
if handleutils.RequestLog(
cmp_err,
"",
http.StatusNotFound,
&w,
) {
return
}

Expand Down
100 changes: 100 additions & 0 deletions stiller-backend/internal/handlers/file/filedl/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package filedl

import (
"net/http"
"stiller/internal/dbutils"
"stiller/internal/fsutils"
"stiller/internal/handlers/handleutils"
"stiller/internal/jwtutils"

jsonexp "github.com/go-json-experiment/json"
"github.com/julienschmidt/httprouter"
"github.com/leporo/sqlf"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)


func Nethandler(
w http.ResponseWriter,
r *http.Request,
params httprouter.Params,
) {
if handleutils.CORS(w, r) {
return
}

type ReqPayload struct {
FileId int `json:"file_id"`
}

req_payload := ReqPayload{}
unmarshal_err := jsonexp.UnmarshalRead(
r.Body,
&req_payload,
jsonexp.DefaultOptionsV2(),
)

if handleutils.RequestLog(
unmarshal_err,
"",
http.StatusBadRequest,
&w,
) {
return
}

user_token := r.Header.Get("token")
user_tk, token_decode_err := jwtutils.Decode(user_token)
if handleutils.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
return
}

new_dbconn, conn_err := dbutils.NewConn()
if handleutils.RequestLog(
conn_err,
"",
http.StatusInternalServerError,
&w,
) {
return
}

user_id := user_tk.UserId
getpath_stmt := sqlf.
Select("path").
From("file").
Where("owner = ?", user_id)

getpath_query := getpath_stmt.String()
path := ""

exec_err := sqlitex.ExecuteTransient(
new_dbconn,
getpath_query,
&sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
path = stmt.GetText("path")
return nil
},

Args: getpath_stmt.Args(),
},
)

if handleutils.RequestLog(exec_err, "", http.StatusNotFound, &w) {
return
}

fexists, fexists_err := fsutils.FileExists(path)
if handleutils.RequestLog(fexists_err, "", http.StatusInternalServerError, &w) {
return
}

if !fexists {
handleutils.GenericLog(nil, "path '%s' doesnt exist", path)
}

http.ServeFile(w, r, path)
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fileretrieve
package filetree

import (
"net/http"
Expand Down
26 changes: 18 additions & 8 deletions stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"stiller/internal/handlers/auth/newuser"
"stiller/internal/handlers/auth/userlogin"
"stiller/internal/handlers/auth/userverify"
"stiller/internal/handlers/file/fileretrieve"
"stiller/internal/handlers/file/filedl"
filetree "stiller/internal/handlers/file/filetree"
"stiller/internal/handlers/file/patchfile"
"stiller/internal/handlers/file/upload"
"stiller/internal/handlers/gallery/addtemplate"
Expand All @@ -31,19 +32,24 @@ var routes = [...]individualHandler{
handlefunc: addtemplate.NetHandler,
},
{
path: "/file/upload",
method: http.MethodPost,
handlefunc: upload.NetHandler,
path: "/file",
method: http.MethodGet,
handlefunc: filetree.Nethandler,
},
{
path: "/file/update",
path: "/file",
method: http.MethodPatch,
handlefunc: patchfile.NetHandler,
},
{
path: "/file/retrieveall",
path: "/file/new",
method: http.MethodPost,
handlefunc: upload.NetHandler,
},
{
path: "/file/dl",
method: http.MethodGet,
handlefunc: fileretrieve.Nethandler,
handlefunc: filedl.Nethandler,
},
{
path: "/auth/newuser",
Expand Down Expand Up @@ -77,7 +83,11 @@ func routerDigest(router *httprouter.Router, ind *individualHandler) {
fun = router.PUT
}

router.OPTIONS(ind.path, ind.handlefunc)
_, _, flag := router.Lookup(http.MethodOptions, ind.path)
if flag {
router.OPTIONS(ind.path, ind.handlefunc)
}

fun(ind.path, ind.handlefunc)
}

Expand Down

0 comments on commit 31f648c

Please sign in to comment.