Give uploads one path end to end, and serve /uploads/ from Garage first
Frontend: src/uploads.js is the one way to store or show a file (uploadFile, ACCEPT, kindOf, isUploadUrl, tagFor, mediaSize). Chat, the box editor, canvas drops and the background editor all use it; views/home3/upload.js and the chat store's own axios call are gone. Backend: POST /api/uploads (was /messages/upload) is a thin wrapper over services.Uploads.Save, which owns validation, naming and storage. The three per-extension maps are now one UploadTypes table, and a refused file is an *UploadError whose message is safe to show. nginx now fetches /uploads/ from Garage first and falls back to the volume on a 404 or any 5xx, so the bucket is the source of truth and deleting a file in garage-webui unpublishes it, while a Garage outage still only affects bucket-only files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { FITS, useBackgroundStore } from "@/stores/background";
|
||||
import { uploadFile, isImage } from "./upload";
|
||||
import { ACCEPT, uploadFile, isImage } from "@/uploads";
|
||||
import OverlayShell from "@/components/util/OverlayShell.vue";
|
||||
import ConfirmDialog from "@/components/util/ConfirmDialog.vue";
|
||||
|
||||
@@ -133,7 +133,7 @@ async function confirmReset() {
|
||||
<span class="row">
|
||||
<label class="bar-btn upload">
|
||||
{{ busy ? "Working…" : "Upload image…" }}
|
||||
<input type="file" accept="image/*" hidden :disabled="busy" @change="upload($event.target.files[0])" />
|
||||
<input type="file" :accept="ACCEPT.images" hidden :disabled="busy" @change="upload($event.target.files[0])" />
|
||||
</label>
|
||||
<span class="hint">or drop one on this panel</span>
|
||||
</span>
|
||||
|
||||
@@ -39,7 +39,7 @@ import HtmlBox from "./HtmlBox.vue";
|
||||
import WidgetBox from "./WidgetBox.vue";
|
||||
import { WIDGETS } from "./widgetRegistry";
|
||||
import { TEMPLATES, imageHtml, videoHtml } from "./templates";
|
||||
import { mediaSize, uploadFile } from "./upload";
|
||||
import { mediaSize, uploadFile } from "@/uploads";
|
||||
|
||||
const props = defineProps({
|
||||
page: { type: String, required: true },
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* the panel. Saving every keystroke would push half-written markup to
|
||||
* every visitor; leaving is the point it is finished.
|
||||
*
|
||||
* "Insert upload" uploads a file (upload.js) and puts a tag for it at the
|
||||
* "Insert upload" uploads a file (@/uploads) and puts a tag for it at the
|
||||
* cursor — the way to add pictures to an existing box, e.g. a gallery.
|
||||
*
|
||||
* The panel is teleported to <body>, so it is fixed to the window whatever
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { useBoxesStore } from "@/stores/boxes";
|
||||
import { tagFor, uploadFile } from "./upload";
|
||||
import { ACCEPT, tagFor, uploadFile } from "@/uploads";
|
||||
import CodeEditor from "./CodeEditor.vue";
|
||||
import { WIDGETS } from "./widgetRegistry";
|
||||
import OverlayShell from "@/components/util/OverlayShell.vue";
|
||||
@@ -194,7 +194,7 @@ function close() {
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
accept="image/*,video/mp4,video/webm,audio/mpeg,audio/ogg"
|
||||
:accept="ACCEPT.media"
|
||||
hidden
|
||||
@change="upload"
|
||||
/>
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
/**
|
||||
* Uploading a file for use in a box: stores it through the chat's upload
|
||||
* endpoint (public, unguessable URLs under /uploads/) and says what markup
|
||||
* shows it. Used by BoxEditor's "Insert upload" and by dropping files onto
|
||||
* the canvas in edit mode.
|
||||
*
|
||||
* Never the private upload directory: that one is behind a cookie check, and
|
||||
* a box's sandboxed frame loads as an opaque origin, so its requests may not
|
||||
* carry the cookie.
|
||||
*/
|
||||
import { api } from "@/api";
|
||||
|
||||
/** Upload one file; resolves to its URL. Throws an ApiError on failure. */
|
||||
export async function uploadFile(file) {
|
||||
const body = new FormData();
|
||||
body.append("file", file);
|
||||
const { url } = await api.post("/messages/upload", body, { fallback: `Upload of ${file.name} failed` });
|
||||
return url;
|
||||
}
|
||||
|
||||
export const isImage = (url) => /\.(jpe?g|png|gif|webp)$/i.test(url);
|
||||
export const isVideo = (url) => /\.(mp4|webm)$/i.test(url);
|
||||
|
||||
/** The markup an uploaded file is shown with, by type. Anything else (a PDF,
|
||||
* a text file) becomes a link. */
|
||||
export function tagFor(url) {
|
||||
if (isImage(url)) return `<img src="${url}" alt="">`;
|
||||
if (isVideo(url)) return `<video src="${url}" controls loop muted></video>`;
|
||||
if (/\.(mp3|ogg)$/i.test(url)) return `<audio src="${url}" controls></audio>`;
|
||||
return `<a href="${url}" target="_blank">${url.split("/").pop()}</a>`;
|
||||
}
|
||||
|
||||
/** Natural size of an image or video file, or null if it can't be read. */
|
||||
export function mediaSize(file) {
|
||||
return new Promise((resolve) => {
|
||||
const src = URL.createObjectURL(file);
|
||||
const done = (size) => {
|
||||
URL.revokeObjectURL(src);
|
||||
resolve(size);
|
||||
};
|
||||
if (file.type.startsWith("video/")) {
|
||||
const v = document.createElement("video");
|
||||
v.onloadedmetadata = () => done({ w: v.videoWidth, h: v.videoHeight });
|
||||
v.onerror = () => done(null);
|
||||
v.src = src;
|
||||
} else {
|
||||
const img = new Image();
|
||||
img.onload = () => done({ w: img.naturalWidth, h: img.naturalHeight });
|
||||
img.onerror = () => done(null);
|
||||
img.src = src;
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user