add download handler

This commit is contained in:
rmanach 2025-01-07 11:53:14 +01:00
parent 5e805b5b4d
commit ee52a1b4f2
6 changed files with 68 additions and 10 deletions

View File

@ -8,9 +8,5 @@ build: lint
lint: lint:
golangci-lint run --fix golangci-lint run --fix
# .run-meilisearch:
# docker-compose up -d meilisearch
run: lint run: lint
# while [ "`curl --insecure -s -o /dev/null -w ''%{http_code}'' http://localhost:7700/health`" != "200" ]; do sleep 2; echo "waiting..."; done
go run main.go go run main.go

View File

@ -0,0 +1,47 @@
package download
import (
"fmt"
"librapi/services"
"net/http"
"path/filepath"
"github.com/rs/zerolog/log"
)
const URL = "/download"
func Handler(bs services.IStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getDownload(w, r, bs)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
}
func getDownload(w http.ResponseWriter, r *http.Request, bs services.IStore) {
queryParams := r.URL.Query()
downloadFiles, ok := queryParams["file"]
if !ok {
log.Error().Msg("file query param does not exist")
http.Error(w, "file does not exists", http.StatusBadRequest)
return
}
if len(downloadFiles) != 1 {
log.Error().Msg("only one file is allowed to download")
http.Error(w, "only one file is allowed to download", http.StatusBadRequest)
return
}
filename := downloadFiles[0]
filePath := filepath.Join(bs.GetStoreDir(), filename)
http.ServeFile(w, r, filePath)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
w.Header().Set("Content-Type", "application/pdf")
}

View File

@ -12,6 +12,7 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"librapi/handlers/download"
"librapi/handlers/home" "librapi/handlers/home"
"librapi/handlers/login" "librapi/handlers/login"
"librapi/handlers/upload" "librapi/handlers/upload"
@ -64,6 +65,7 @@ func main() {
server.NewHandler(home.URL, home.Handler(bs)), server.NewHandler(home.URL, home.Handler(bs)),
server.NewHandler(upload.URL, upload.Handler(auth, bs)), server.NewHandler(upload.URL, upload.Handler(auth, bs)),
server.NewHandler(login.URL, login.Handler(auth)), server.NewHandler(login.URL, login.Handler(auth)),
server.NewHandler(download.URL, download.Handler(bs)),
) )
srv.Serve() srv.Serve()

View File

@ -24,6 +24,7 @@ var (
type IStore interface { type IStore interface {
Save(bm *BookMetadata, content io.ReadCloser) error Save(bm *BookMetadata, content io.ReadCloser) error
Search(value string) ([]BookMetadata, error) Search(value string) ([]BookMetadata, error)
GetStoreDir() string
} }
var _ IStore = (*BookStore)(nil) var _ IStore = (*BookStore)(nil)
@ -168,6 +169,10 @@ func (bs *BookStore) Done() <-chan struct{} {
return chDone return chDone
} }
func (bs *BookStore) GetStoreDir() string {
return bs.dir
}
func (bs *BookStore) Save(bm *BookMetadata, content io.ReadCloser) error { func (bs *BookStore) Save(bm *BookMetadata, content io.ReadCloser) error {
bs.processing.Add(1) bs.processing.Add(1)
defer bs.processing.Done() defer bs.processing.Done()

View File

@ -1,5 +1,5 @@
{{ define "content" }} {{ define "content" }}
<h3>A simple API to store, search and download books.</h3> <h3>A simple API to store, search and download books, articles, etc...</h3>
<div>No extra JS, CSS or fancy stuff. You click, you search, you found, you're happy.</div> <div>No extra JS, CSS or fancy stuff. You click, you search, you found, you're happy.</div>
<form action="/home" method="post" enctype="multipart/form-data"> <form action="/home" method="post" enctype="multipart/form-data">
<div class="main-container"> <div class="main-container">
@ -27,17 +27,19 @@
<table> <table>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Description</th>
<th>Editor</th> <th>Editor</th>
<th>Authors</th> <th>Authors</th>
<th>Year</th> <th>Year</th>
</tr> </tr>
{{range .Results}} {{range .Results}}
<tr> <tr>
<td><div style="margin: 10px;">{{.Name}}</div></td> <td><div style="margin: 10px; word-wrap: break-word; width: 300px;">{{.Name}}</div></td>
<td><div style="margin: 10px; word-wrap: break-word; width: 300px;">{{.Description | noDesc}}</div></td>
<td style="text-align: center;"><div style="margin: 10px;">{{.Editor}}</div></td> <td style="text-align: center;"><div style="margin: 10px;">{{.Editor}}</div></td>
<td style="text-align: center;"><div style="margin: 10px;">{{.Authors | join }}</div></td> <td style="text-align: center; word-wrap: break-word; width: 300px;"><div style="margin: 10px;">{{.Authors | join }}</div></td>
<td><div style="margin: 20px;">{{.Year}}</div></td> <td><div style="margin: 20px;">{{.Year}}</div></td>
<td><div style="margin: 20px;">{{.Path | bookUrl}}</div></td> <td><div style="margin: 20px;"><a target="_blank" href="{{.Path | bookUrl}}">Download</a></div></td>
</tr> </tr>
{{ end }} {{ end }}
</table> </table>

View File

@ -56,7 +56,13 @@ var funcMap = template.FuncMap{
}, },
"bookUrl": func(path string) string { "bookUrl": func(path string) string {
_, filename := filepath.Split(path) _, filename := filepath.Split(path)
return fmt.Sprintf("https://books.thegux.fr/downloads/%s", filename) return fmt.Sprintf("/download?file=%s", filename)
},
"noDesc": func(desc *string) string {
if desc == nil {
return ""
}
return *desc
}, },
} }