-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
158 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file was deleted.
Oops, something went wrong.