Skip to content

Commit

Permalink
add download logic and test (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol authored Jul 9, 2024
1 parent 4fc25df commit 89bd661
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:latest

WORKDIR /app
COPY go.mod .
# COPY go.sum .
COPY go.sum .
RUN mkdir upload
RUN go mod download
COPY . .
Expand Down
4 changes: 2 additions & 2 deletions core/logging/request_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func LogRequest(r *http.Request) {
// skip Authorization header
continue
}
slog.Info("[Request Header]", name, h)
slog.Debug("[Request Header]", name, h)
}
}

Expand All @@ -26,5 +26,5 @@ func LogRequest(r *http.Request) {
return
}
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
slog.Info("[Request Body]" + string(bodyBytes))
slog.Debug("[Request Body]" + string(bodyBytes))
}
4 changes: 2 additions & 2 deletions core/util/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/tttol/mos3/core/model"
)

func GenerateS3Objects(r *http.Request, dir string, path string) ([]model.S3Object, error) {
dirEntry, err := os.ReadDir(filepath.Join(dir, path))
func GenerateS3Objects(r *http.Request, dir string, dirPath string) ([]model.S3Object, error) {
dirEntry, err := os.ReadDir(filepath.Join(dir, dirPath))
if err != nil {
slog.Error("ReadDir error", "error", err)
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/tttol/mos3

go 1.21.3

require github.com/go-playground/assert/v2 v2.2.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
41 changes: 41 additions & 0 deletions web/rename_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package web

import (
"net/http"
"os"
"path/filepath"
)

func RenameHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}

oldFilename := r.FormValue("oldFilename")
newFilename := r.FormValue("newFilename")
err := os.Rename(filepath.Join(UPLOAD_DIR, oldFilename), filepath.Join(UPLOAD_DIR, newFilename))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Redirect(w, r, "/", http.StatusFound)
}

func RenamedirHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}

oldDirname := r.FormValue("oldDirname")
newDirname := r.FormValue("newDirname")
err := os.Rename(filepath.Join(UPLOAD_DIR, oldDirname), filepath.Join(UPLOAD_DIR, newDirname))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Redirect(w, r, "/", http.StatusFound)
}
96 changes: 96 additions & 0 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package web

import (
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"regexp"
"text/template"

"github.com/tttol/mos3/core/util"
)

func S3Handler(w http.ResponseWriter, r *http.Request) {
slog.Info("S3Handler is called.")
path := r.URL.Path[len("/s3/"):]

s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR, GetDirPath(path))
if err != nil {
slog.Error("GenerateS3Objects error", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var currentPath string
if path == "" {
currentPath = "/"
} else {
currentPath = path
}

dataMap := map[string]interface{}{
"S3Objects": s3Objects,
"Breadcrumbs": util.GenerateBreadcrumbs(path),
"CurrentPath": currentPath,
}

tmpl, err := template.ParseFiles("static/index.html")
if err != nil {
slog.Error("template file error", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if r.URL.Query().Get("action") == "dl" {
_, status, err := download(w, path)
if err != nil {
http.Error(w, err.Error(), status)
return
}
}

slog.Info("dataMap", "dataMap", dataMap)
tmpl.Execute(w, dataMap)
}

// It returns the number of bytes copied and the first error encountered while copying, if any.
func download(w http.ResponseWriter, path string) (n int64, httpStatus int, err error) {
slog.Info("Start downloading file", "path", path)

file, err := os.Open(filepath.Join(UPLOAD_DIR, path))
if err != nil {
slog.Error("File open error", "error", err)
return 0, http.StatusNotFound, err
}
defer file.Close()

stat, err := file.Stat()
if err != nil {
slog.Error("File stat error", "error", err)
return 0, http.StatusNotFound, err
}

w.Header().Set("Content-Disposition", "attachment; filename="+stat.Name())
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", string(rune(stat.Size())))

writtenByteSize, err := io.Copy(w, file)
if err != nil {
slog.Error("File copy error", "error", err)
return 0, http.StatusInternalServerError, err
}

slog.Info("Successful to dowload file", "path", path)
return writtenByteSize, http.StatusOK, nil
}

func GetDirPath(path string) string {
re := regexp.MustCompile(`\.\w+$`)
if re.MatchString(path) {
return filepath.Dir(path)
} else {
return path
}
}
12 changes: 12 additions & 0 deletions web/s3_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package web

import (
"testing"

"github.com/go-playground/assert/v2"
)

func TestGetDirPath(t *testing.T) {
assert.Equal(t, GetDirPath("/hoge/fuga"), "/hoge/fuga")
assert.Equal(t, GetDirPath("/hoge/fuga/piyo.txt"), "/hoge/fuga")
}
90 changes: 0 additions & 90 deletions web/ui_controller.go

This file was deleted.

0 comments on commit 89bd661

Please sign in to comment.