initial commit
This commit is contained in:
190
main.go
Normal file
190
main.go
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
IsDir bool
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
ModifiedAt string
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveFile(w http.ResponseWriter, r *http.Request, filePath string) error {
|
||||||
|
imgFormats := []string{".png", ".jpeg", ".jpg", ".bmp", ".svg", ".gif", ".avif"}
|
||||||
|
videoFormats := []string{".mp4", ".webm"}
|
||||||
|
|
||||||
|
for _, format := range imgFormats {
|
||||||
|
if strings.HasSuffix(filePath, format) {
|
||||||
|
http.ServeFile(w, r, filePath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, format := range videoFormats {
|
||||||
|
if strings.HasSuffix(filePath, format) {
|
||||||
|
http.ServeFile(w, r, filePath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(filePath, ".pdf") {
|
||||||
|
http.ServeFile(w, r, filePath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
data, err := io.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.Write(data)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
directory string
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.StringVar(&directory, "dir", "./", "directory to list files from")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
tmpl := template.Must(template.ParseGlob("./views/**"))
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sortValue := r.URL.Query().Get("sort")
|
||||||
|
if sortValue == "" {
|
||||||
|
sortValue = "oldest"
|
||||||
|
}
|
||||||
|
|
||||||
|
directoriesFirst := r.URL.Query().Has("directoriesFirst")
|
||||||
|
|
||||||
|
suffix := r.URL.Path
|
||||||
|
dirPath := path.Join(directory, suffix)
|
||||||
|
|
||||||
|
stat, err := os.Stat(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !stat.IsDir() {
|
||||||
|
err = serveFile(w, r, dirPath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := os.ReadDir(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entries := []Entry{}
|
||||||
|
for _, file := range files {
|
||||||
|
info, err := file.Info()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error reading info for file %s: %v\n", file.Name(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
entries = append(entries, Entry{
|
||||||
|
IsDir: file.IsDir(),
|
||||||
|
Name: file.Name(),
|
||||||
|
Path: path.Join(suffix, file.Name()),
|
||||||
|
ModifiedAt: info.ModTime().Format(time.DateTime),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(entries, func(a, b Entry) int {
|
||||||
|
if directoriesFirst {
|
||||||
|
if a.IsDir && !b.IsDir {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if b.IsDir && !a.IsDir {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t1, err := time.Parse(time.DateTime, a.ModifiedAt)
|
||||||
|
t2, err := time.Parse(time.DateTime, b.ModifiedAt)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
switch sortValue {
|
||||||
|
case "oldest":
|
||||||
|
return t1.Compare(t2)
|
||||||
|
case "newest":
|
||||||
|
return t2.Compare(t1)
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
tmpl.ExecuteTemplate(w, "index.html", struct {
|
||||||
|
Entries []Entry
|
||||||
|
}{entries})
|
||||||
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("POST /delete", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files := []string{}
|
||||||
|
for k, v := range r.Form {
|
||||||
|
if k == "files" {
|
||||||
|
files = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, filePath := range files {
|
||||||
|
fullPath := path.Join(directory, filePath)
|
||||||
|
|
||||||
|
stat, err := os.Stat(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if stat.IsDir() {
|
||||||
|
continue // for now
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(fullPath); err != nil {
|
||||||
|
fmt.Printf("couldn't delete file %s: %v\n", fullPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := http.ListenAndServe(":5000", mux); err != nil {
|
||||||
|
log.Fatalf("failed to start http server: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
readme.md
Normal file
3
readme.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# downloads-manager
|
||||||
|
|
||||||
|
Main feature is to list files and directories inside ~/Downloads folder and show each one's last modification date, user can select files to delete
|
||||||
160
views/index.html
Normal file
160
views/index.html
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Downloads Manager</title>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.container {
|
||||||
|
max-width: 1920px;
|
||||||
|
width: 100%;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
max-width: 900px;
|
||||||
|
width: 100%;
|
||||||
|
margin: 24px auto;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#delete-form {
|
||||||
|
max-width: 900px;
|
||||||
|
width: 100%;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item {
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
gap: 16px;
|
||||||
|
padding: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item:hover {
|
||||||
|
background-color: #aaa;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item a {
|
||||||
|
all: unset;
|
||||||
|
cursor: pointer;
|
||||||
|
color: #000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item_directory a {
|
||||||
|
color: blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item__left {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submit-btn {
|
||||||
|
width: 100%;
|
||||||
|
border: none;
|
||||||
|
background-color: #b00420;
|
||||||
|
color: #fff;
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 12px;
|
||||||
|
border-radius: 6px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1 class="title">Downloads Manager</h1>
|
||||||
|
|
||||||
|
<div class="header">
|
||||||
|
<div>
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="directories-first" />
|
||||||
|
Directories first
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<select name="sort">
|
||||||
|
<option value="oldest" selected>Oldest</option>
|
||||||
|
<option value="newest">Newest</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form method="post" action="/delete" id="delete-form">
|
||||||
|
<ul class="list">
|
||||||
|
{{range .Entries}}
|
||||||
|
<li class="item {{if .IsDir}}item_directory{{else}}item_file{{end}}">
|
||||||
|
<div class="item__left">
|
||||||
|
<input type="checkbox" name="files" value="{{.Path}}" />
|
||||||
|
<a href="{{.Path}}">{{.Name}}</a>
|
||||||
|
</div>
|
||||||
|
<span>{{.ModifiedAt}}</span>
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
<button type="submit" class="submit-btn">Delete selected files</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const deleteForm = document.querySelector("form#delete-form");
|
||||||
|
deleteForm.addEventListener("submit", (e) => {
|
||||||
|
const ok = confirm("are you sure?");
|
||||||
|
if (!ok) {
|
||||||
|
e.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const directoriesCheckbox = document.querySelector(
|
||||||
|
'input[name="directories-first"]',
|
||||||
|
);
|
||||||
|
const sortSelect = document.querySelector('select[name="sort"]');
|
||||||
|
|
||||||
|
directoriesCheckbox.addEventListener("change", (e) => {
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
if (e.target.checked) {
|
||||||
|
params.set("directoriesFirst", "");
|
||||||
|
} else {
|
||||||
|
params.delete("directoriesFirst");
|
||||||
|
}
|
||||||
|
location.search = params.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
sortSelect.addEventListener("change", (e) => {
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
params.set("sort", e.target.value);
|
||||||
|
location.search = params.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener("load", () => {
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
if (params.has("directoriesFirst")) {
|
||||||
|
directoriesCheckbox.checked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.has("sort")) {
|
||||||
|
sortSelect.value = params.get("sort");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user