Skip to content

Commit

Permalink
add uploadpage
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 1, 2024
1 parent a59f6cc commit 8ef3892
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 166 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-emerald-950 text-white">
<header class="bg-emerald-400 text-white p-4 text-center font-black text-4xl">MOS3</header>
<header class="bg-emerald-400 text-white p-4 text-center font-black text-4xl"><a href="/s3">MOS3</a></header>
<main class="p-3">
<form action="/mkdir" method="post">
<input type="text" name="dirname" placeholder="Directory name">
Expand All @@ -23,14 +23,13 @@
{{end}}
</div>
<div id="upload" class="inline-block bg-blue-500 text-white w-auto font-bold py-2 px-2 rounded-full mb-3 hover:opacity-60">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 inline">
<path fillRule="evenodd" d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z" clipRule="evenodd" />
</svg>
New File
<a href="/uploadpage">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 inline">
<path fillRule="evenodd" d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z" clipRule="evenodd" />
</svg>
New File
</a>
</div>
<form id="uploadForm" class="hidden inline" action="/upload" method="post" enctype="multipart/form-data">
<input id="uploadFile" type="file" name="file">
</form>
</div>
{{range .S3Objects}}
<div class="border-b-2 border-slate-400 py-3 hover:opacity-60">
Expand Down
22 changes: 22 additions & 0 deletions static/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MOS3 - Upload file</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-emerald-950 text-white">
<header class="bg-emerald-400 text-white p-4 text-center font-black text-4xl"><a href="/s3">MOS3</a></header>
<main class="p-3">
<div class="mx-auto w-3/4 p-3 bg-emerald-800 rounded-xl">
<div class="flex justify-between">
<form id="uploadForm" class="inline" action="/upload" method="post" enctype="multipart/form-data">
<input id="uploadFile" type="file" name="file">
<input type="hidden" name="currentPath" value="{{.CurrentPath}}">
</form>
</div>
</div>
</main>
</body>
</html>
115 changes: 1 addition & 114 deletions web/controller.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package web

import (
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/tttol/mos3/core/amazon/awscli"
"github.com/tttol/mos3/core/amazon/awssdk"
"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)

Expand Down Expand Up @@ -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)
}
43 changes: 0 additions & 43 deletions web/s3_controller.go

This file was deleted.

129 changes: 129 additions & 0 deletions web/ui_controller.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 8ef3892

Please sign in to comment.