diff --git a/main.go b/main.go index fd95b0a..a63429b 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,9 @@ import ( ) func main() { - http.HandleFunc("/", web.IndexHandler) + http.HandleFunc("/", web.CliSdkHandler) http.HandleFunc("/s3/", web.S3Handler) + http.HandleFunc("/uploadpage", web.UploadIndexHandler) http.HandleFunc("/upload", web.UploadHandler) http.HandleFunc("/delete", web.DeleteHandler) http.HandleFunc("/rename", web.RenameHandler) diff --git a/static/index.html b/static/index.html index 1e2d8cb..6c63849 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -
MOS3
+
MOS3
@@ -23,14 +23,13 @@ {{end}}
- - - - New File + + + + + New File +
- - -
{{range .S3Objects}}
diff --git a/static/upload.html b/static/upload.html new file mode 100644 index 0000000..09821ed --- /dev/null +++ b/static/upload.html @@ -0,0 +1,22 @@ + + + + + + MOS3 - Upload file + + + +
MOS3
+
+
+
+
+ + +
+
+
+
+ + diff --git a/web/controller.go b/web/controller.go index 77690e6..86b3f16 100644 --- a/web/controller.go +++ b/web/controller.go @@ -1,11 +1,8 @@ package web import ( - "io" "log/slog" "net/http" - "os" - "path/filepath" "strings" "github.com/tttol/mos3/core/amazon/awscli" @@ -13,7 +10,7 @@ import ( "github.com/tttol/mos3/core/logging" ) -func IndexHandler(w http.ResponseWriter, r *http.Request) { +func CliSdkHandler(w http.ResponseWriter, r *http.Request) { slog.Info("IndexHandler is called.") logging.LogRequest(r) @@ -48,113 +45,3 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { } } } - -func UploadHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) - return - } - - file, header, err := r.FormFile("file") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer file.Close() - - dst, err := os.Create(filepath.Join(UPLOAD_DIR, header.Filename)) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer dst.Close() - - if _, err := io.Copy(dst, file); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - http.Redirect(w, r, "/", http.StatusFound) -} - -func DeleteHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) - return - } - - filename := r.FormValue("filename") - err := os.Remove(filepath.Join(UPLOAD_DIR, filename)) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - http.Redirect(w, r, "/", http.StatusFound) -} - -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 MkdirHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) - return - } - - dirname := r.FormValue("dirname") - err := os.Mkdir(filepath.Join(UPLOAD_DIR, dirname), os.ModePerm) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - http.Redirect(w, r, "/", http.StatusFound) -} - -func RmdirHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) - return - } - - dirname := r.FormValue("dirname") - err := os.Remove(filepath.Join(UPLOAD_DIR, dirname)) - 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) -} diff --git a/web/s3_controller.go b/web/s3_controller.go deleted file mode 100644 index 328e80d..0000000 --- a/web/s3_controller.go +++ /dev/null @@ -1,43 +0,0 @@ -package web - -import ( - "log/slog" - "net/http" - "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/"):] - if r.URL.Query().Get("action") == "dl" { - download(path) - return - } - - s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR, path) - if err != nil { - slog.Error("GetS3Objects error", "error", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - dataMap := map[string]interface{}{ - "S3Objects": s3Objects, - "Breadcrumbs": util.GenerateBreadcrumbs(path), - } - - 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 - } - - slog.Info("dataMap", "dataMap", dataMap) - tmpl.Execute(w, dataMap) -} -func download(path string) { - slog.Info("Download file", "path", path) -} diff --git a/web/ui_controller.go b/web/ui_controller.go new file mode 100644 index 0000000..ca32d4b --- /dev/null +++ b/web/ui_controller.go @@ -0,0 +1,129 @@ +package web + +import ( + "log/slog" + "net/http" + "os" + "path/filepath" + "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/"):] + if r.URL.Query().Get("action") == "dl" { + download(path) + return + } + + s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR, path) + if err != nil { + slog.Error("GenerateS3Objects error", "error", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + dataMap := map[string]interface{}{ + "S3Objects": s3Objects, + "Breadcrumbs": util.GenerateBreadcrumbs(path), + "CurrentPath": path, + } + + 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 + } + + slog.Info("dataMap", "dataMap", dataMap) + tmpl.Execute(w, dataMap) +} + +func DeleteHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + filename := r.FormValue("filename") + err := os.Remove(filepath.Join(UPLOAD_DIR, filename)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/", http.StatusFound) +} + +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 MkdirHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + dirname := r.FormValue("dirname") + err := os.Mkdir(filepath.Join(UPLOAD_DIR, dirname), os.ModePerm) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/", http.StatusFound) +} + +func RmdirHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + dirname := r.FormValue("dirname") + err := os.Remove(filepath.Join(UPLOAD_DIR, dirname)) + 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) +} + +func download(path string) { + slog.Info("Download file", "path", path) +} diff --git a/web/upload_controller.go b/web/upload_controller.go new file mode 100644 index 0000000..f18d4ea --- /dev/null +++ b/web/upload_controller.go @@ -0,0 +1,49 @@ +package web + +import ( + "io" + "log/slog" + "net/http" + "os" + "path/filepath" + "text/template" +) + +func UploadIndexHandler(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseFiles("static/upload.html") + if err != nil { + slog.Error("template file error", "error", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl.Execute(w, nil) +} +func UploadHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + path := r.FormValue("currentPath") + file, header, err := r.FormFile("file") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer file.Close() + + dst, err := os.Create(filepath.Join(UPLOAD_DIR, path, header.Filename)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer dst.Close() + + if _, err := io.Copy(dst, file); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/", http.StatusFound) +}