Add managed Craft addons with secure installation and document editing
This commit is contained in:
1 parent
1afa755d4f
commit
5cb1b463ea
31 files changed
+2274
-15
No files matched your search
@@ -7,7 +7,11 @@
|
||||
/>
|
||||
|
||||
<errors v-if="error" :errorCode="error.status" />
|
||||
<component v-else-if="currentView" :is="currentView"></component>
|
||||
<component
|
||||
v-else-if="currentView"
|
||||
:is="currentView"
|
||||
:key="route.query.craft ? route.fullPath : undefined"
|
||||
></component>
|
||||
<div v-else>
|
||||
<h2 class="message delayed">
|
||||
<div class="spinner">
|
||||
@@ -46,6 +50,9 @@ import { name } from "../utils/constants";
|
||||
|
||||
const Editor = defineAsyncComponent(() => import("@/views/files/Editor.vue"));
|
||||
const Preview = defineAsyncComponent(() => import("@/views/files/Preview.vue"));
|
||||
const CraftEditor = defineAsyncComponent(
|
||||
() => import("@/views/files/CraftEditor.vue")
|
||||
);
|
||||
|
||||
const layoutStore = useLayoutStore();
|
||||
const fileStore = useFileStore();
|
||||
@@ -61,6 +68,7 @@ let fetchDataController = new AbortController();
|
||||
const error = ref<StatusError | null>(null);
|
||||
|
||||
const currentView = computed(() => {
|
||||
if (route.query.craft && layoutStore.loading) return null;
|
||||
if (fileStore.req?.type === undefined) {
|
||||
return null;
|
||||
}
|
||||
@@ -68,6 +76,8 @@ const currentView = computed(() => {
|
||||
if (fileStore.req.isDir) {
|
||||
return FileListing;
|
||||
}
|
||||
if (["gridcraft", "pdfcraft"].includes(String(route.query.craft)))
|
||||
return CraftEditor;
|
||||
if (
|
||||
route.query.edit === "true" &&
|
||||
["text", "textImmutable"].includes(fileStore.req.type)
|
||||
@@ -100,6 +110,10 @@ watch(route, () => {
|
||||
fetchData();
|
||||
});
|
||||
watch(reload, (newValue) => {
|
||||
if (newValue && route.query.craft) {
|
||||
fileStore.reload = false;
|
||||
return;
|
||||
}
|
||||
newValue && fetchData();
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,409 @@
|
||||
<template>
|
||||
<div class="craft-editor">
|
||||
<header-bar
|
||||
><Action
|
||||
icon="close"
|
||||
:label="t('buttons.close')"
|
||||
@action="close"
|
||||
/><title>{{ currentName }}</title></header-bar
|
||||
>
|
||||
<div class="craft-status" role="status">
|
||||
<strong>{{ title }}</strong
|
||||
><span>{{
|
||||
saving
|
||||
? t("addons.saving")
|
||||
: pending
|
||||
? t("addons.unsaved")
|
||||
: frameDirty
|
||||
? t("addons.unsaved")
|
||||
: status
|
||||
}}</span
|
||||
><button
|
||||
v-if="pending && !saveAs && !saving"
|
||||
type="button"
|
||||
class="button button--flat"
|
||||
@click="persist"
|
||||
>
|
||||
{{ t("addons.retrySave") }}</button
|
||||
><button
|
||||
v-if="pending && !saveAs && !saving"
|
||||
type="button"
|
||||
class="button button--flat"
|
||||
@click="
|
||||
newName = pending.name;
|
||||
saveAs = true;
|
||||
"
|
||||
>
|
||||
{{ t("addons.saveAs") }}</button
|
||||
><button
|
||||
v-if="pending"
|
||||
type="button"
|
||||
class="button button--flat"
|
||||
@click="downloadCopy"
|
||||
>
|
||||
{{ t("addons.downloadCopy") }}
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="error" class="craft-error" role="alert">
|
||||
{{ error }}
|
||||
<a
|
||||
v-if="authExpired"
|
||||
:href="baseURL + '/auth/login'"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>{{ t("sidebar.login") }}</a
|
||||
>
|
||||
</p>
|
||||
<form v-if="saveAs" class="craft-save-as" @submit.prevent="confirmSaveAs">
|
||||
<label
|
||||
>{{ t("addons.saveAs")
|
||||
}}<input class="input" v-model.trim="newName" required /></label
|
||||
><button class="button" :disabled="saving">{{ t("buttons.save") }}</button
|
||||
><button
|
||||
type="button"
|
||||
class="button button--flat"
|
||||
@click="saveAs = false"
|
||||
>
|
||||
{{ t("buttons.cancel") }}
|
||||
</button>
|
||||
</form>
|
||||
<iframe
|
||||
v-if="frameURL"
|
||||
ref="frame"
|
||||
class="craft-frame"
|
||||
:src="frameURL"
|
||||
:title="title"
|
||||
allow="clipboard-read; clipboard-write; fullscreen"
|
||||
@load="connectActivity"
|
||||
sandbox="allow-scripts allow-same-origin allow-downloads allow-modals allow-popups"
|
||||
/>
|
||||
<p v-else-if="!error" role="status">{{ t("files.loading") }}</p>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, onMounted, onBeforeUnmount } from "vue";
|
||||
import {
|
||||
useRoute,
|
||||
useRouter,
|
||||
onBeforeRouteLeave,
|
||||
onBeforeRouteUpdate,
|
||||
} from "vue-router";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import HeaderBar from "@/components/header/HeaderBar.vue";
|
||||
import Action from "@/components/header/Action.vue";
|
||||
import { useFileStore } from "@/stores/file";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useAddonsStore } from "@/stores/addons";
|
||||
import { baseURL } from "@/utils/constants";
|
||||
import { fetchURL, StatusError } from "@/api/utils";
|
||||
import { craftMessage, safeCraftName } from "@/utils/craft";
|
||||
const route = useRoute(),
|
||||
router = useRouter(),
|
||||
store = useAddonsStore(),
|
||||
files = useFileStore(),
|
||||
auth = useAuthStore(),
|
||||
{ t } = useI18n();
|
||||
// Bind the editor to the file opened at mount; later store navigation must not redirect a save.
|
||||
const file = files.req,
|
||||
id = String(route.query.craft || ""),
|
||||
sourcePath = file?.path || "";
|
||||
const currentPath = ref(sourcePath),
|
||||
currentName = ref(file?.name || "");
|
||||
const title = computed(
|
||||
() => store.items.find((item) => item.id === id)?.name || id
|
||||
);
|
||||
const frame = ref<HTMLIFrameElement>(),
|
||||
frameURL = ref(""),
|
||||
error = ref(""),
|
||||
status = ref(""),
|
||||
frameDirty = ref(false),
|
||||
saving = ref(false),
|
||||
saveAs = ref(false),
|
||||
newName = ref("");
|
||||
interface Save {
|
||||
bytes: ArrayBuffer;
|
||||
name: string;
|
||||
path: string;
|
||||
create: boolean;
|
||||
revision: number;
|
||||
}
|
||||
const pending = ref<Save>();
|
||||
const authExpired = ref(false);
|
||||
let activityDocument: Document | null = null,
|
||||
lastActivity = 0;
|
||||
const activityEvents = [
|
||||
"keydown",
|
||||
"pointerdown",
|
||||
"input",
|
||||
"wheel",
|
||||
"touchstart",
|
||||
];
|
||||
function disconnectActivity() {
|
||||
for (const name of activityEvents)
|
||||
activityDocument?.removeEventListener(name, activity, true);
|
||||
activityDocument = null;
|
||||
}
|
||||
function connectActivity() {
|
||||
disconnectActivity();
|
||||
activityDocument = frame.value?.contentDocument || null;
|
||||
for (const name of activityEvents)
|
||||
activityDocument?.addEventListener(name, activity, {
|
||||
capture: true,
|
||||
passive: true,
|
||||
});
|
||||
}
|
||||
function activity(event: Event) {
|
||||
if (
|
||||
!event.isTrusted ||
|
||||
document.visibilityState !== "visible" ||
|
||||
Date.now() - lastActivity < 60000
|
||||
)
|
||||
return;
|
||||
lastActivity = Date.now();
|
||||
fetchURL("/api/session", { signal: controller.signal }, false)
|
||||
.then((response) => {
|
||||
authExpired.value = false;
|
||||
response.body?.cancel();
|
||||
})
|
||||
.catch((failure) => {
|
||||
if (failure instanceof StatusError && failure.status === 401) {
|
||||
authExpired.value = true;
|
||||
error.value = t("addons.sessionExpired");
|
||||
}
|
||||
});
|
||||
}
|
||||
let tag = "",
|
||||
revision = 0,
|
||||
disposed = false,
|
||||
opened = false,
|
||||
startup: ReturnType<typeof setTimeout> | undefined;
|
||||
const controller = new AbortController();
|
||||
let original: ArrayBuffer | undefined;
|
||||
function endpoint(path: string) {
|
||||
return "/api/craft/" + id + "/document?path=" + encodeURIComponent(path);
|
||||
}
|
||||
const unsaved = computed(
|
||||
() => !!pending.value || saving.value || frameDirty.value
|
||||
);
|
||||
function mayLeave() {
|
||||
return !unsaved.value || window.confirm(t("addons.leaveUnsaved"));
|
||||
}
|
||||
onBeforeRouteLeave(mayLeave);
|
||||
onBeforeRouteUpdate(mayLeave);
|
||||
function beforeUnload(event: BeforeUnloadEvent) {
|
||||
if (unsaved.value) event.preventDefault();
|
||||
}
|
||||
function close() {
|
||||
router.push({
|
||||
path: file?.url.slice(0, file.url.lastIndexOf("/") + 1) || "/files/",
|
||||
});
|
||||
}
|
||||
async function persist() {
|
||||
const payload = pending.value;
|
||||
if (!payload || saving.value) return;
|
||||
saving.value = true;
|
||||
error.value = "";
|
||||
let success = false;
|
||||
try {
|
||||
const response = await fetchURL(
|
||||
endpoint(payload.path),
|
||||
{
|
||||
method: "PUT",
|
||||
headers: payload.create
|
||||
? { "If-None-Match": "*" }
|
||||
: { "If-Match": tag },
|
||||
body: payload.bytes,
|
||||
signal: controller.signal,
|
||||
},
|
||||
false
|
||||
);
|
||||
if (disposed) return;
|
||||
success = true;
|
||||
authExpired.value = false;
|
||||
if (
|
||||
payload.path.toLowerCase().endsWith(id === "gridcraft" ? ".xlsx" : ".pdf")
|
||||
) {
|
||||
tag = response.headers.get("ETag") || "";
|
||||
currentPath.value = payload.path;
|
||||
currentName.value = payload.name;
|
||||
}
|
||||
if (pending.value === payload) {
|
||||
pending.value = undefined;
|
||||
status.value = t("addons.saved");
|
||||
if (revision === payload.revision) frameDirty.value = false;
|
||||
}
|
||||
} catch (failure) {
|
||||
if (!disposed) {
|
||||
if (failure instanceof StatusError && failure.status === 401)
|
||||
authExpired.value = true;
|
||||
error.value =
|
||||
failure instanceof StatusError &&
|
||||
[409, 412].includes(failure.status || 0)
|
||||
? t("addons.conflict")
|
||||
: t("addons.saveFailed") + " " + String(failure);
|
||||
}
|
||||
} finally {
|
||||
saving.value = false;
|
||||
if (success && pending.value && pending.value !== payload && !saveAs.value)
|
||||
queueMicrotask(persist);
|
||||
}
|
||||
}
|
||||
function confirmSaveAs() {
|
||||
if (!pending.value) return;
|
||||
const ext = pending.value.name.toLowerCase().endsWith(".pdf")
|
||||
? ".pdf"
|
||||
: id === "gridcraft"
|
||||
? ".xlsx"
|
||||
: ".pdf";
|
||||
const name = safeCraftName(newName.value, ext);
|
||||
if (!name) {
|
||||
error.value = t("addons.invalidName");
|
||||
return;
|
||||
}
|
||||
pending.value = {
|
||||
...pending.value,
|
||||
name,
|
||||
path:
|
||||
currentPath.value.slice(0, currentPath.value.lastIndexOf("/") + 1) + name,
|
||||
create: true,
|
||||
};
|
||||
saveAs.value = false;
|
||||
persist();
|
||||
}
|
||||
function downloadCopy() {
|
||||
if (!pending.value) return;
|
||||
const url = URL.createObjectURL(new Blob([pending.value.bytes]));
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = pending.value.name;
|
||||
link.click();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
function receive(event: MessageEvent) {
|
||||
const message = craftMessage(
|
||||
event,
|
||||
frame.value?.contentWindow || null,
|
||||
id,
|
||||
window.location.origin
|
||||
);
|
||||
if (!message) return;
|
||||
if (message.type === id + ":ready") {
|
||||
if (opened || !original) return;
|
||||
opened = true;
|
||||
clearTimeout(startup);
|
||||
frame.value!.contentWindow!.postMessage(
|
||||
{ type: id + ":open", name: file!.name, bytes: original },
|
||||
window.location.origin,
|
||||
[original]
|
||||
);
|
||||
original = undefined;
|
||||
status.value = t("addons.loaded");
|
||||
} else if (message.type === id + ":failed") {
|
||||
clearTimeout(startup);
|
||||
error.value = String(message.error || t("addons.startFailed"));
|
||||
} else if (message.type === id + ":dirty") {
|
||||
frameDirty.value = message.dirty === true;
|
||||
if (frameDirty.value) revision++;
|
||||
} else if (message.type === id + ":save") {
|
||||
if (
|
||||
!(message.bytes instanceof ArrayBuffer) ||
|
||||
message.bytes.byteLength > 64 * 1024 * 1024
|
||||
) {
|
||||
error.value = t("addons.tooLarge");
|
||||
return;
|
||||
}
|
||||
const extension = message.name?.toLowerCase().endsWith(".pdf")
|
||||
? ".pdf"
|
||||
: id === "gridcraft"
|
||||
? ".xlsx"
|
||||
: ".pdf";
|
||||
const name = safeCraftName(message.name, extension);
|
||||
if (!name) {
|
||||
error.value = t("addons.invalidName");
|
||||
return;
|
||||
}
|
||||
const create = !!message.saveAs || name !== currentName.value;
|
||||
pending.value = {
|
||||
bytes: message.bytes,
|
||||
name,
|
||||
path: currentPath.value,
|
||||
create,
|
||||
revision,
|
||||
};
|
||||
if (create) {
|
||||
newName.value = name;
|
||||
saveAs.value = true;
|
||||
} else persist();
|
||||
}
|
||||
}
|
||||
onMounted(async () => {
|
||||
window.addEventListener("message", receive);
|
||||
window.addEventListener("beforeunload", beforeUnload);
|
||||
try {
|
||||
await store.refresh();
|
||||
const addon = store.items.find(
|
||||
(item) => item.id === id && item.enabled && item.build
|
||||
);
|
||||
if (
|
||||
!addon ||
|
||||
!auth.user?.perm.modify ||
|
||||
!auth.user?.perm.download ||
|
||||
!sourcePath.toLowerCase().endsWith(addon.extension)
|
||||
)
|
||||
throw new Error(t("addons.unavailable"));
|
||||
// Bound streamed data even when Content-Length is missing.
|
||||
const response = await fetchURL(endpoint(sourcePath), {
|
||||
signal: controller.signal,
|
||||
});
|
||||
tag = response.headers.get("ETag") || "";
|
||||
if (!tag) throw new Error("Missing document version");
|
||||
if (Number(response.headers.get("Content-Length")) > 64 * 1024 * 1024) {
|
||||
await response.body?.cancel();
|
||||
throw new Error(t("addons.tooLarge"));
|
||||
}
|
||||
const reader = response.body?.getReader();
|
||||
if (!reader) throw new Error("Empty document");
|
||||
let length = 0;
|
||||
const chunks: Uint8Array[] = [];
|
||||
while (true) {
|
||||
const part = await reader.read();
|
||||
if (part.done) break;
|
||||
length += part.value.length;
|
||||
if (length > 64 * 1024 * 1024) {
|
||||
await reader.cancel();
|
||||
throw new Error(t("addons.tooLarge"));
|
||||
}
|
||||
chunks.push(part.value);
|
||||
}
|
||||
const data = new Uint8Array(length);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
data.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
original = data.buffer;
|
||||
if (disposed) return;
|
||||
frameURL.value =
|
||||
baseURL +
|
||||
"/addons/" +
|
||||
id +
|
||||
"/" +
|
||||
addon.build +
|
||||
"/index.html?host=parent&author=" +
|
||||
encodeURIComponent(auth.user.profileName || auth.user.username);
|
||||
startup = setTimeout(() => {
|
||||
error.value = t("addons.startFailed");
|
||||
}, 45000);
|
||||
} catch (failure) {
|
||||
if (!disposed) error.value = String(failure);
|
||||
}
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
disconnectActivity();
|
||||
disposed = true;
|
||||
controller.abort();
|
||||
clearTimeout(startup);
|
||||
window.removeEventListener("message", receive);
|
||||
window.removeEventListener("beforeunload", beforeUnload);
|
||||
});
|
||||
</script>
|
||||
@@ -176,6 +176,13 @@
|
||||
:pos="contextMenuPos"
|
||||
@hide="hideContextMenu"
|
||||
>
|
||||
<CraftAction
|
||||
:file="
|
||||
fileStore.selectedCount === 1
|
||||
? fileStore.req?.items[fileStore.selected[0]]
|
||||
: null
|
||||
"
|
||||
/>
|
||||
<action
|
||||
v-if="headerButtons.share"
|
||||
icon="share"
|
||||
@@ -284,6 +291,7 @@ import Search from "@/components/Search.vue";
|
||||
import Item from "@/components/files/ListingItem.vue";
|
||||
import CreateMenu from "@/components/files/CreateMenu.vue";
|
||||
import ContextMenu from "@/components/ContextMenu.vue";
|
||||
import CraftAction from "@/components/files/CraftAction.vue";
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
/>
|
||||
|
||||
<template #actions>
|
||||
<CraftAction :file="fileStore.req" />
|
||||
<action
|
||||
:disabled="layoutStore.loading"
|
||||
v-if="authStore.user?.perm.rename"
|
||||
@@ -179,6 +180,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import CraftAction from "@/components/files/CraftAction.vue";
|
||||
import { useStorage } from "@vueuse/core";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useFileStore } from "@/stores/file";
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
><small>{{ t("explorer.usersHint") }}</small></span
|
||||
><AppIcon name="chevron_right" />
|
||||
</button>
|
||||
<Addons />
|
||||
<section class="card settings-section">
|
||||
<h2><AppIcon name="archive" />{{ t("settings.downloadsTitle") }}</h2>
|
||||
<label class="settings-field"
|
||||
@@ -131,6 +132,7 @@
|
||||
<script setup lang="ts">
|
||||
import { useRoute } from "vue-router";
|
||||
import { settings as api } from "@/api";
|
||||
import Addons from "@/components/settings/Addons.vue";
|
||||
import { StatusError } from "@/api/utils";
|
||||
import Rules from "@/components/settings/Rules.vue";
|
||||
import Themes from "@/components/settings/Themes.vue";
|
||||
|
||||
Reference in new issue
Block a user