Skip to content

Commit

Permalink
add dir.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jun 30, 2024
1 parent fa3166f commit ab53659
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
7 changes: 7 additions & 0 deletions core/model/s3_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type S3Object struct {
FullPath string
Name string
IsDir bool
}
32 changes: 32 additions & 0 deletions core/util/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

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

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

func GetS3Objects(r *http.Request, path string) ([]model.S3Object, error) {
dirEntry, err := os.ReadDir(filepath.Join("./upload", path))
if err != nil {
slog.Error("ReadDir error", "error", err)
return nil, err
}

var s3Objects []model.S3Object
for _, entry := range dirEntry {
var obj model.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)

return s3Objects, nil
}
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
<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="{{.FullPath}}">{{.Name}}</a></span>
<span><a href="{{.FullPath}}" class="text-lg">{{.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="{{.FullPath}}?action=dl">{{.Name}}</a></span>
<span><a href="{{.FullPath}}?action=dl" class="text-lg">{{.Name}}</a></span>
{{end}}
</p>
</div>
Expand Down
25 changes: 4 additions & 21 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package web
import (
"log/slog"
"net/http"
"os"
"path/filepath"
"text/template"

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

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/"):]
Expand All @@ -24,24 +18,13 @@ func S3Handler(w http.ResponseWriter, r *http.Request) {
return
}

dirEntry, err := os.ReadDir(filepath.Join(uploadDir, path))
s3Objects, err := util.GetS3Objects(r, path)
if err != nil {
slog.Error("GetS3Objects error", "error", err)
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)
Expand Down

0 comments on commit ab53659

Please sign in to comment.