- store/files.go: pastas (parent, anti-ciclo), FileMeta (nota opcional = avulso),
blobs com extensão neutralizada p/ tipos perigosos, sanitização de nome
- api/files.go: upload multipart (64MiB), tree, static com ETag/Range + CSP
sandbox + attachment p/ html/svg/js (stored-XSS mitigado), move/rename/delete,
CRUD de pastas
- web: overlay Ctrl+J / 📎 (modo seletor insere sintaxe md/html/txt e associa),
filtros Todos/Anexos/Avulsos/Nota atual, busca, lightbox img/video/audio/pdf/texto,
colar-e-arrastar no editor e no gerenciador, ações por card (preview/associar/
mover/renomear/excluir), mobile-first (ações sempre visíveis)
- fix: api() já serializa o body — chamadas do gerenciador passavam JSON pré-string
(dupla codificação); auth por cookie (sem Bearer manual no fetch)
- sw v10; testes store+api (traversal, ciclo, XSS headers, range); README
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Gerenciador de arquivos: upload, árvore, static seguro, mover/renomear/excluir
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// handleFileUpload recebe multipart/form-data com campo "file" e parâmetros
|
|
// opcionais folder_id e note_id.
|
|
func (s *Server) handleFileUpload(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseMultipartForm(8 << 20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "multipart inválido: "+err.Error())
|
|
return
|
|
}
|
|
defer r.MultipartForm.RemoveAll()
|
|
f, hdr, err := r.FormFile("file")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, `campo "file" é obrigatório`)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
meta, err := s.st.AddFile(hdr.Filename, r.FormValue("folder_id"), r.FormValue("note_id"), f)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, map[string]any{
|
|
"id": meta.ID, "name": meta.Name, "folder": meta.Folder, "note": meta.Note,
|
|
"size": meta.Size, "mime": meta.MIME, "sha256": meta.SHA256,
|
|
"created_at": meta.CreatedAt, "url": "/api/files/static/" + meta.ID,
|
|
})
|
|
}
|
|
|
|
// handleFileTree devolve a hierarquia de pastas com arquivos + raiz solta.
|
|
func (s *Server) handleFileTree(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"tree": s.st.FileTree(),
|
|
"rootFiles": s.st.RootFiles(),
|
|
})
|
|
}
|
|
|
|
func (s *Server) handleFileStatic(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.st.ServeFile(w, r, r.PathValue("id")); err != nil {
|
|
mapStoreError(w, err)
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleFileDelete(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.st.DeleteFile(r.PathValue("id")); err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
type fileMoveInput struct {
|
|
Folder *string `json:"folder"` // ausente = mantém; "" = raiz
|
|
Note *string `json:"note"` // ausente = mantém; "" = avulso
|
|
Name string `json:"name"` // opcional (renomear junto)
|
|
}
|
|
|
|
func (s *Server) handleFileMove(w http.ResponseWriter, r *http.Request) {
|
|
var in fileMoveInput
|
|
if r.ContentLength > 0 {
|
|
if err := decodeJSON(r, &in, 1<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
}
|
|
m, err := s.st.MoveFile(r.PathValue("id"), in.Name, in.Folder, in.Note)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"id": m.ID, "name": m.Name, "folder": m.Folder, "note": m.Note,
|
|
"size": m.Size, "mime": m.MIME, "sha256": m.SHA256,
|
|
"created_at": m.CreatedAt, "url": "/api/files/static/" + m.ID,
|
|
})
|
|
}
|
|
|
|
type folderInput struct {
|
|
Name string `json:"name"`
|
|
Parent *string `json:"parent"`
|
|
}
|
|
|
|
func (s *Server) handleFolderCreate(w http.ResponseWriter, r *http.Request) {
|
|
var in folderInput
|
|
if err := decodeJSON(r, &in, 1<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
parent := ""
|
|
if in.Parent != nil {
|
|
parent = *in.Parent
|
|
}
|
|
f, err := s.st.CreateFolder(in.Name, parent)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, f)
|
|
}
|
|
|
|
func (s *Server) handleFolderUpdate(w http.ResponseWriter, r *http.Request) {
|
|
var in folderInput
|
|
if err := decodeJSON(r, &in, 1<<20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
|
|
return
|
|
}
|
|
f, err := s.st.UpdateFolder(r.PathValue("id"), in.Name, in.Parent)
|
|
if err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, f)
|
|
}
|
|
|
|
func (s *Server) handleFolderDelete(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.st.DeleteFolder(r.PathValue("id")); err != nil {
|
|
mapStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|