- go.mod module path (and every internal/vibetree import) inventree -> vibetree - cmd/inventree/ -> cmd/vibetree/, deploy/inventree.service -> vibetree.service - Default site name (seed data + the siteName template func's fallback) is now "vibetree", shown in the navbar, <title>, and login page - Session cookie renamed inventree_session -> vibetree_session - Default DB path data/inventree.db -> data/vibetree.db, CLI/log text, Dockerfile, docker-compose.yml, README, .gitignore/.dockerignore all updated to match Existing installs: this changes the session cookie name (users will be signed out once) and the default DB filename (point -db at your existing file, or rename it, to keep your data).
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"vibetree/internal/assets"
|
|
)
|
|
|
|
func (a *App) handleExportCSV(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
filename := fmt.Sprintf("vibetree-assets-%s.csv", time.Now().Format("2006-01-02"))
|
|
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
|
|
|
|
assetIDsParam := r.URL.Query().Get("asset_ids")
|
|
folderIDsParam := r.URL.Query().Get("folder_ids")
|
|
if assetIDsParam != "" || folderIDsParam != "" {
|
|
// A multi-select batch (an arbitrary mix of individually-picked
|
|
// assets and whole folders) doesn't map onto a single ListFilter.
|
|
seen := map[int64]bool{}
|
|
var ids []int64
|
|
for _, id := range parseIDList(assetIDsParam) {
|
|
if !seen[id] {
|
|
seen[id] = true
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
for _, fid := range parseIDList(folderIDsParam) {
|
|
batch, _, err := a.Assets.List(ctx, assets.ListFilter{FolderID: &fid, FolderSet: true, IncludeSub: true, Page: 1, PageSize: 2000})
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, as := range batch {
|
|
if !seen[as.ID] {
|
|
seen[as.ID] = true
|
|
ids = append(ids, as.ID)
|
|
}
|
|
}
|
|
}
|
|
if err := a.Assets.ExportByIDs(ctx, w, ids); err != nil {
|
|
a.Log.Error("csv export failed", "error", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
filter := assets.ListFilter{
|
|
ItemTypeID: queryInt64(r, "item_type_id"),
|
|
StatusID: queryInt64(r, "status_id"),
|
|
AssignedUserID: queryInt64(r, "assigned_user_id"),
|
|
Query: r.URL.Query().Get("q"),
|
|
}
|
|
if folderID, err := queryOptionalID(r, "folder_id"); err == nil && folderID != nil {
|
|
filter.FolderID = folderID
|
|
filter.FolderSet = true
|
|
filter.IncludeSub = r.URL.Query().Get("recursive") == "1"
|
|
}
|
|
if err := a.Assets.Export(ctx, w, filter); err != nil {
|
|
a.Log.Error("csv export failed", "error", err)
|
|
}
|
|
}
|
|
|
|
// handleImportCSV backs the folder-scoped "Import CSV..." context-menu
|
|
// action: every row imports directly into the given folder_id, so the CSV
|
|
// needs no Folder column at all. It's only ever invoked from within a
|
|
// specific folder (never at the top level) and responds with JSON since
|
|
// it's called via fetch from the tree/panel context menu.
|
|
func (a *App) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
|
jsonErr(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
file, _, err := r.FormFile("file")
|
|
if err != nil {
|
|
jsonErr(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
opts := assets.ImportOptions{}
|
|
if folderID, err := formOptionalID(r, "folder_id"); err == nil && folderID != nil {
|
|
opts.TargetFolderID = folderID
|
|
}
|
|
|
|
itemTypes, err := a.ItemTypes.List(ctx, true)
|
|
if err != nil {
|
|
jsonErr(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
statusList, err := a.Statuses.List(ctx)
|
|
if err != nil {
|
|
jsonErr(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
userList, err := a.Users.List(ctx)
|
|
if err != nil {
|
|
jsonErr(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
result, err := a.Assets.Import(ctx, file, opts, itemTypes, statusList, userList)
|
|
if err != nil {
|
|
jsonErr(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
jsonOK(w, result)
|
|
}
|