Skip to content

Commit

Permalink
fix s3_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jun 30, 2024
1 parent ea1cd7a commit fa3166f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
File renamed without changes.
24 changes: 20 additions & 4 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@
<button type="submit">Create Directory</button>
</form>

<div class="p-3 bg-emerald-800 rounded-xl">
<div class="mx-auto w-3/4 p-3 bg-emerald-800 rounded-xl">
<div class="flex justify-between">
<div class="text-lg">
<a href="/s3" class="text-cyan-400 underline-offset-2 hover:opacity-60">s3://</a>
{{range .}}
{{if .IsDir}}
<span> / <a href="{{.FullPath}}" class="text-cyan-400 underline-offset-2 hover:opacity-60">{{.Name}}</a></span>
{{end}}
{{end}}
</div>
<div class="inline-block bg-green-400 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
</div>
</div>
{{range .}}
<div class="border-b-2 border-slate-400 py-3">
<div class="border-b-2 border-slate-400 py-3 hover:opacity-60">
<p>
{{if .IsDir}}
<span class="pl-2 mr-2">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 inline">
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.75V12A2.25 2.25 0 0 1 4.5 9.75h15A2.25 2.25 0 0 1 21.75 12v.75m-8.69-6.44-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z" />
</svg>
</span>
<span><a href="{{.Name}}">{{.Name}}</a></span>
<span><a href="{{.FullPath}}">{{.Name}}</a></span>
{{else}}
<span class="pl-2 mr-2">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="size-6 inline">
<path d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625Z" />
<path d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z" />
</svg>
</span>
<span><a href="{{.Name}}?action=dl">{{.Name}}</a></span>
<span><a href="{{.FullPath}}?action=dl">{{.Name}}</a></span>
{{end}}
</p>
</div>
Expand Down
25 changes: 0 additions & 25 deletions web/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"os"
"path/filepath"
"strings"
"text/template"

"github.com/tttol/mos3/core/amazon/awscli"
"github.com/tttol/mos3/core/amazon/awssdk"
"github.com/tttol/mos3/core/logging"
)

const uploadDir = "./upload"

func IndexHandler(w http.ResponseWriter, r *http.Request) {
slog.Info("IndexHandler is called.")
Expand Down Expand Up @@ -52,29 +50,6 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
}
}

func S3Handler(w http.ResponseWriter, r *http.Request) {
slog.Info("S3Handler is called.")
path := r.URL.Path[len("/s3/"):]
files, err := os.ReadDir(filepath.Join(uploadDir, path))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if r.URL.Query().Get("ation") == "dl" {
slog.Info("Download file", "path", 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
}

tmpl.Execute(w, files)
}

func UploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
Expand Down
56 changes: 56 additions & 0 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package web

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

const uploadDir = "./upload"

type S3Object struct {
FullPath string
Name string
IsDir bool
}

func S3Handler(w http.ResponseWriter, r *http.Request) {
slog.Info("S3Handler is called.")
path := r.URL.Path[len("/s3/"):]
if r.URL.Query().Get("ation") == "dl" {
download(path)
return
}

dirEntry, err := os.ReadDir(filepath.Join(uploadDir, path))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var s3Objects []S3Object
for _, entry := range dirEntry {
var obj S3Object
obj.Name = entry.Name()
obj.FullPath = filepath.Join(r.URL.Path, entry.Name())
obj.IsDir = entry.IsDir()
slog.Info("S3Object", "data", obj)

s3Objects = append(s3Objects, obj)
}
slog.Info("S3Objects", "data", s3Objects)

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
}

tmpl.Execute(w, s3Objects)
}
func download(path string) {
slog.Info("Download file", "path", path)
}

0 comments on commit fa3166f

Please sign in to comment.