add download handler
This commit is contained in:
parent
5e805b5b4d
commit
ee52a1b4f2
4
Makefile
4
Makefile
@ -8,9 +8,5 @@ build: lint
|
||||
lint:
|
||||
golangci-lint run --fix
|
||||
|
||||
# .run-meilisearch:
|
||||
# docker-compose up -d meilisearch
|
||||
|
||||
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
|
||||
47
handlers/download/handler.go
Normal file
47
handlers/download/handler.go
Normal 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")
|
||||
}
|
||||
2
main.go
2
main.go
@ -12,6 +12,7 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"librapi/handlers/download"
|
||||
"librapi/handlers/home"
|
||||
"librapi/handlers/login"
|
||||
"librapi/handlers/upload"
|
||||
@ -64,6 +65,7 @@ func main() {
|
||||
server.NewHandler(home.URL, home.Handler(bs)),
|
||||
server.NewHandler(upload.URL, upload.Handler(auth, bs)),
|
||||
server.NewHandler(login.URL, login.Handler(auth)),
|
||||
server.NewHandler(download.URL, download.Handler(bs)),
|
||||
)
|
||||
srv.Serve()
|
||||
|
||||
|
||||
@ -24,6 +24,7 @@ var (
|
||||
type IStore interface {
|
||||
Save(bm *BookMetadata, content io.ReadCloser) error
|
||||
Search(value string) ([]BookMetadata, error)
|
||||
GetStoreDir() string
|
||||
}
|
||||
|
||||
var _ IStore = (*BookStore)(nil)
|
||||
@ -168,6 +169,10 @@ func (bs *BookStore) Done() <-chan struct{} {
|
||||
return chDone
|
||||
}
|
||||
|
||||
func (bs *BookStore) GetStoreDir() string {
|
||||
return bs.dir
|
||||
}
|
||||
|
||||
func (bs *BookStore) Save(bm *BookMetadata, content io.ReadCloser) error {
|
||||
bs.processing.Add(1)
|
||||
defer bs.processing.Done()
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
{{ 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>
|
||||
<form action="/home" method="post" enctype="multipart/form-data">
|
||||
<div class="main-container">
|
||||
@ -27,17 +27,19 @@
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Description</th>
|
||||
<th>Editor</th>
|
||||
<th>Authors</th>
|
||||
<th>Year</th>
|
||||
</tr>
|
||||
{{range .Results}}
|
||||
<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;">{{.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;">{{.Path | bookUrl}}</div></td>
|
||||
<td><div style="margin: 20px;"><a target="_blank" href="{{.Path | bookUrl}}">Download</a></div></td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
@ -39,7 +39,7 @@ var funcMap = template.FuncMap{
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
} else {
|
||||
return strings.Join(s, ",")
|
||||
return strings.Join(s, ", ")
|
||||
}
|
||||
},
|
||||
"filename": func(h *multipart.FileHeader) string {
|
||||
@ -56,7 +56,13 @@ var funcMap = template.FuncMap{
|
||||
},
|
||||
"bookUrl": func(path string) string {
|
||||
_, 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
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user