Modernize icons and file previews; add bounded ZIP directory viewer
This commit is contained in:
1 parent
9110c8a9d5
commit
2366035c72
67 files changed
+1657
-658
No files matched your search
@@ -37,7 +37,7 @@
|
||||
/>
|
||||
<div class="encoding-list">
|
||||
<div v-if="encodingList.length == 0" class="message">
|
||||
<i class="material-icons">sentiment_dissatisfied</i>
|
||||
<AppIcon name="sentiment_dissatisfied" />
|
||||
<span>{{ $t("files.lonely") }}</span>
|
||||
</div>
|
||||
<button
|
||||
@@ -55,11 +55,11 @@
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="displayError" class="csv-error">
|
||||
<i class="material-icons">error</i>
|
||||
<AppIcon name="error" />
|
||||
<p>{{ displayError }}</p>
|
||||
</div>
|
||||
<div v-else-if="parsed.headers.length === 0" class="csv-empty">
|
||||
<i class="material-icons">description</i>
|
||||
<AppIcon name="description" />
|
||||
<p>{{ $t("files.lonely") }}</p>
|
||||
</div>
|
||||
<div v-else class="csv-table-container" @wheel.stop @touchmove.stop>
|
||||
@@ -81,7 +81,7 @@
|
||||
</table>
|
||||
<div class="csv-footer">
|
||||
<div class="csv-info" v-if="parsed.rows.length > 100">
|
||||
<i class="material-icons">info</i>
|
||||
<AppIcon name="info" />
|
||||
<span>
|
||||
{{ $t("files.showingRows", { count: parsed.rows.length }) }}
|
||||
</span>
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
<template>
|
||||
<svg
|
||||
class="dicom-file-icon"
|
||||
viewBox="0 0 48 56"
|
||||
fill="none"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path
|
||||
d="M8 2h21l13 13v37a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2Z"
|
||||
fill="currentColor"
|
||||
fill-opacity=".12"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
/>
|
||||
<path
|
||||
d="M28 2v14h14"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linejoin="round"
|
||||
/>
|
||||
<text
|
||||
x="24"
|
||||
y="37"
|
||||
text-anchor="middle"
|
||||
fill="currentColor"
|
||||
font-family="system-ui, sans-serif"
|
||||
font-weight="700"
|
||||
font-size="10"
|
||||
>
|
||||
.dcm
|
||||
</text>
|
||||
</svg>
|
||||
</template>
|
||||
@@ -0,0 +1,12 @@
|
||||
<template>
|
||||
<div class="resource-epub">
|
||||
<VueReader
|
||||
:url="source"
|
||||
:epubOptions="{ allowScriptedContent: false, allowPopups: false }"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { VueReader } from "vue-reader";
|
||||
defineProps<{ source: string }>();
|
||||
</script>
|
||||
@@ -10,7 +10,12 @@
|
||||
@mouseup="mouseUp"
|
||||
@wheel="wheelMove"
|
||||
>
|
||||
<img class="image-ex-img image-ex-img-center" ref="imgex" @load="onLoad" />
|
||||
<img
|
||||
class="image-ex-img image-ex-img-center"
|
||||
ref="imgex"
|
||||
@load="onLoad"
|
||||
@error="emit('error')"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
@@ -30,6 +35,7 @@ const props = withDefaults(defineProps<IProps>(), {
|
||||
classList: () => [],
|
||||
zoomStep: () => 0.25,
|
||||
});
|
||||
const emit = defineEmits<{ (event: "error"): void }>();
|
||||
|
||||
const scale = ref<number>(1);
|
||||
const lastX = ref<number | null>(null);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<template>
|
||||
<AppIcon v-if="directory" name="folder" class="file-icon" />
|
||||
<svg
|
||||
v-else
|
||||
class="app-icon file-icon"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="1.4"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M5 2h9l5 5v15H5z M14 2v6h5" />
|
||||
<text
|
||||
v-if="extension"
|
||||
x="12"
|
||||
y="17"
|
||||
text-anchor="middle"
|
||||
fill="currentColor"
|
||||
stroke="none"
|
||||
font-size="3.8"
|
||||
font-weight="650"
|
||||
font-family="system-ui, sans-serif"
|
||||
>
|
||||
{{ extension }}
|
||||
</text>
|
||||
<path v-else d="M8 12h8 M8 16h6" />
|
||||
</svg>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import AppIcon from "../AppIcon.vue";
|
||||
const props = defineProps<{ name: string; directory?: boolean }>();
|
||||
const extension = computed(
|
||||
() =>
|
||||
props.name
|
||||
.match(/\.([^.]+)$/)?.[0]
|
||||
.slice(0, 5)
|
||||
.toLowerCase() || ""
|
||||
);
|
||||
</script>
|
||||
@@ -25,7 +25,7 @@
|
||||
@contextmenu="contextMenu"
|
||||
>
|
||||
<div class="file-symbol">
|
||||
<DicomIcon v-if="isDicom" />
|
||||
<FileIcon v-if="isDicom" :name="name" />
|
||||
<img
|
||||
v-else-if="showThumbnail && thumbnailVisible && !thumbnailFailed"
|
||||
:src="thumbnailUrl"
|
||||
@@ -33,7 +33,7 @@
|
||||
decoding="async"
|
||||
@error="thumbnailFailed = true"
|
||||
/>
|
||||
<i v-else class="material-icons"></i>
|
||||
<FileIcon v-else :name="name" :directory="isDir" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -60,7 +60,7 @@ import dayjs from "dayjs";
|
||||
import { files as api } from "@/api";
|
||||
import * as upload from "@/utils/upload";
|
||||
import { computed, inject, ref, watch, onMounted, onBeforeUnmount } from "vue";
|
||||
import DicomIcon from "./DicomIcon.vue";
|
||||
import FileIcon from "./FileIcon.vue";
|
||||
import { useRouter } from "vue-router";
|
||||
|
||||
const touches = ref<number>(0);
|
||||
|
||||
@@ -34,6 +34,13 @@
|
||||
<option :value="1.5">150%</option>
|
||||
<option :value="2">200%</option>
|
||||
</select>
|
||||
<a
|
||||
:href="source"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="pdf-open-native"
|
||||
><AppIcon name="external" />{{ t("documents.openNewTab") }}</a
|
||||
>
|
||||
</div>
|
||||
<p v-if="error" role="alert" class="document-error">{{ error }}</p>
|
||||
<p v-else-if="busy">{{ t("files.loading") }}</p>
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
<template>
|
||||
<PdfViewer v-if="kind === 'pdf'" :source="source" />
|
||||
<DicomViewer v-else-if="kind === 'dicom'" :source="source" />
|
||||
<ZipPreview v-else-if="kind === 'zip'" :source="source" :size="size || 0" />
|
||||
<div
|
||||
v-else
|
||||
class="resource-preview"
|
||||
@wheel.stop
|
||||
@touchmove.stop
|
||||
@keydown.stop
|
||||
>
|
||||
<div v-if="failed || !kind" class="preview-unavailable">
|
||||
<FileIcon :name="name" />
|
||||
<p>{{ t("files.noPreview") }}</p>
|
||||
<small v-if="message" role="alert">{{ message }}</small>
|
||||
</div>
|
||||
<img
|
||||
v-else-if="kind === 'image'"
|
||||
:src="source"
|
||||
:alt="name"
|
||||
class="resource-image"
|
||||
@error="failed = true"
|
||||
/>
|
||||
<audio
|
||||
v-else-if="kind === 'audio'"
|
||||
:src="source"
|
||||
controls
|
||||
preload="metadata"
|
||||
@error="failed = true"
|
||||
/>
|
||||
<video
|
||||
v-else-if="kind === 'video'"
|
||||
:src="source"
|
||||
controls
|
||||
preload="metadata"
|
||||
@error="failed = true"
|
||||
/>
|
||||
<EpubPreview v-else-if="kind === 'epub'" :source="source" />
|
||||
<template v-else>
|
||||
<p v-if="busy" role="status">{{ t("files.loading") }}</p>
|
||||
<img
|
||||
v-else-if="kind === 'tiff' && imageURL"
|
||||
:src="imageURL"
|
||||
:alt="name"
|
||||
class="resource-image"
|
||||
/>
|
||||
<CsvViewer v-else-if="kind === 'csv' && buffer" :content="buffer" />
|
||||
<template v-else-if="buffer">
|
||||
<div class="document-tools">
|
||||
<label
|
||||
>{{ t("files.fileEncoding")
|
||||
}}<select v-model="encoding" class="input">
|
||||
<option value="utf-8">UTF-8</option>
|
||||
<option value="windows-1251">Windows-1251</option>
|
||||
<option value="windows-1256">Windows-1256</option>
|
||||
</select></label
|
||||
>
|
||||
</div>
|
||||
<article
|
||||
v-if="kind === 'markdown'"
|
||||
class="md_preview resource-text"
|
||||
v-html="markdown"
|
||||
/>
|
||||
<pre v-else class="resource-text">{{ text }}</pre>
|
||||
</template>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineAsyncComponent,
|
||||
onBeforeUnmount,
|
||||
ref,
|
||||
watch,
|
||||
} from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { previewBuffer, previewKind } from "@/utils/preview";
|
||||
import FileIcon from "./FileIcon.vue";
|
||||
const PdfViewer = defineAsyncComponent(() => import("./PdfViewer.vue"));
|
||||
const DicomViewer = defineAsyncComponent(() => import("./DicomViewer.vue"));
|
||||
const CsvViewer = defineAsyncComponent(() => import("./CsvViewer.vue"));
|
||||
const EpubPreview = defineAsyncComponent(() => import("./EpubPreview.vue"));
|
||||
const ZipPreview = defineAsyncComponent(() => import("./ZipPreview.vue"));
|
||||
const props = defineProps<{
|
||||
source: string;
|
||||
name: string;
|
||||
type?: string;
|
||||
size?: number;
|
||||
}>();
|
||||
const { t } = useI18n();
|
||||
const kind = computed(() => previewKind(props.name, props.type));
|
||||
const failed = ref(false),
|
||||
busy = ref(false),
|
||||
message = ref(""),
|
||||
encoding = ref("utf-8");
|
||||
const buffer = ref<ArrayBuffer>(),
|
||||
markdown = ref(""),
|
||||
imageURL = ref("");
|
||||
const text = computed(() =>
|
||||
buffer.value ? new TextDecoder(encoding.value).decode(buffer.value) : ""
|
||||
);
|
||||
let controller: AbortController | undefined;
|
||||
function revoke() {
|
||||
if (imageURL.value) URL.revokeObjectURL(imageURL.value);
|
||||
imageURL.value = "";
|
||||
}
|
||||
watch(
|
||||
[() => props.source, kind],
|
||||
async ([source, format]) => {
|
||||
controller?.abort();
|
||||
revoke();
|
||||
buffer.value = undefined;
|
||||
failed.value = false;
|
||||
message.value = "";
|
||||
busy.value = false;
|
||||
if (!format || !["text", "markdown", "csv", "tiff"].includes(format))
|
||||
return;
|
||||
const request = (controller = new AbortController());
|
||||
busy.value = true;
|
||||
try {
|
||||
const limit = format === "tiff" ? 64 * 1024 * 1024 : 5 * 1024 * 1024;
|
||||
if ((props.size || 0) > limit) throw new Error("PREVIEW_TOO_LARGE");
|
||||
const data = await previewBuffer(source, request.signal, limit);
|
||||
if (request.signal.aborted) return;
|
||||
if (format === "tiff") {
|
||||
const { default: UTIF } = await import("utif");
|
||||
if (request.signal.aborted) return;
|
||||
const pages = UTIF.decode(data),
|
||||
page = pages[0];
|
||||
const width = Number(page?.t256?.[0]),
|
||||
height = Number(page?.t257?.[0]);
|
||||
if (
|
||||
!page ||
|
||||
!Number.isSafeInteger(width) ||
|
||||
!Number.isSafeInteger(height) ||
|
||||
width < 1 ||
|
||||
height < 1 ||
|
||||
width * height > 16_000_000
|
||||
)
|
||||
throw new Error("Unsupported TIFF dimensions");
|
||||
UTIF.decodeImage(data, page);
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
canvas
|
||||
.getContext("2d")
|
||||
?.putImageData(
|
||||
new ImageData(
|
||||
new Uint8ClampedArray(UTIF.toRGBA8(page)),
|
||||
width,
|
||||
height
|
||||
),
|
||||
0,
|
||||
0
|
||||
);
|
||||
const blob = await new Promise<Blob | null>((resolve) =>
|
||||
canvas.toBlob(resolve)
|
||||
);
|
||||
if (request.signal.aborted) return;
|
||||
if (!blob) throw new Error("TIFF conversion failed");
|
||||
imageURL.value = URL.createObjectURL(blob);
|
||||
} else buffer.value = data;
|
||||
} catch (error) {
|
||||
if (!request.signal.aborted) {
|
||||
failed.value = true;
|
||||
message.value =
|
||||
error instanceof Error && error.message === "PREVIEW_TOO_LARGE"
|
||||
? t("documents.textTooLarge")
|
||||
: String(error);
|
||||
}
|
||||
} finally {
|
||||
if (controller === request) busy.value = false;
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
let markdownID = 0;
|
||||
watch([text, kind], async ([value, format]) => {
|
||||
const id = ++markdownID;
|
||||
markdown.value = "";
|
||||
if (format !== "markdown") return;
|
||||
const [{ marked }, { default: DOMPurify }] = await Promise.all([
|
||||
import("marked"),
|
||||
import("dompurify"),
|
||||
]);
|
||||
const rendered = DOMPurify.sanitize(await marked(value), {
|
||||
FORBID_TAGS: [
|
||||
"img",
|
||||
"iframe",
|
||||
"object",
|
||||
"embed",
|
||||
"style",
|
||||
"link",
|
||||
"form",
|
||||
"input",
|
||||
"audio",
|
||||
"video",
|
||||
],
|
||||
FORBID_ATTR: ["style"],
|
||||
});
|
||||
if (id === markdownID) markdown.value = rendered;
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
controller?.abort();
|
||||
revoke();
|
||||
markdownID++;
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,101 @@
|
||||
<template>
|
||||
<section class="zip-preview">
|
||||
<p class="zip-preview-note">{{ t("documents.zipTreeOnly") }}</p>
|
||||
<p v-if="busy" role="status">{{ t("files.loading") }}</p>
|
||||
<p v-else-if="error" role="alert">{{ error }}</p>
|
||||
<p v-else-if="!nodes.length">{{ t("documents.zipEmpty") }}</p>
|
||||
<div v-else class="zip-tree">
|
||||
<div
|
||||
v-for="row in rows"
|
||||
:key="row.key"
|
||||
class="zip-tree-row"
|
||||
:style="{ paddingInlineStart: `${12 + row.depth * 16}px` }"
|
||||
>
|
||||
<button
|
||||
v-if="row.node.directory"
|
||||
type="button"
|
||||
:aria-expanded="expanded.has(row.key)"
|
||||
@click="toggle(row.key)"
|
||||
>
|
||||
<AppIcon
|
||||
:name="expanded.has(row.key) ? 'expand_more' : 'chevron_right'"
|
||||
/>
|
||||
<AppIcon :name="expanded.has(row.key) ? 'folder_open' : 'folder'" />
|
||||
<span>{{ row.node.name }}</span>
|
||||
</button>
|
||||
<span v-else class="zip-tree-file"
|
||||
><FileIcon :name="row.node.name" /><span>{{
|
||||
row.node.name
|
||||
}}</span></span
|
||||
>
|
||||
</div>
|
||||
<p v-if="truncated">{{ t("documents.zipVisibleLimit") }}</p>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { zipTree, type ZipNode } from "@/utils/zip-preview";
|
||||
import FileIcon from "./FileIcon.vue";
|
||||
const props = defineProps<{ source: string; size: number }>();
|
||||
const { t } = useI18n();
|
||||
const nodes = ref<ZipNode[]>([]),
|
||||
busy = ref(false),
|
||||
error = ref("");
|
||||
const expanded = ref(new Set<string>());
|
||||
const visible = computed(() => {
|
||||
const rows: { key: string; depth: number; node: ZipNode }[] = [];
|
||||
let truncated = false;
|
||||
function visit(list: ZipNode[], prefix: string, depth: number) {
|
||||
for (const node of list) {
|
||||
if (rows.length >= 1000) {
|
||||
truncated = true;
|
||||
return;
|
||||
}
|
||||
const key = prefix + "/" + node.name;
|
||||
rows.push({ key, depth, node });
|
||||
if (expanded.value.has(key)) visit(node.children, key, depth + 1);
|
||||
}
|
||||
}
|
||||
visit(nodes.value, "", 0);
|
||||
return { rows, truncated };
|
||||
});
|
||||
const rows = computed(() => visible.value.rows),
|
||||
truncated = computed(() => visible.value.truncated);
|
||||
function toggle(key: string) {
|
||||
if (expanded.value.has(key)) expanded.value.delete(key);
|
||||
else expanded.value.add(key);
|
||||
}
|
||||
let controller: AbortController | undefined;
|
||||
watch(
|
||||
[() => props.source, () => props.size],
|
||||
async ([source, size]) => {
|
||||
controller?.abort();
|
||||
const request = (controller = new AbortController());
|
||||
const timeout = setTimeout(() => request.abort(), 30000);
|
||||
nodes.value = [];
|
||||
expanded.value.clear();
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
try {
|
||||
nodes.value = await zipTree(source, size, request.signal);
|
||||
} catch (failure) {
|
||||
if (controller === request)
|
||||
error.value = t(
|
||||
failure instanceof Error && failure.message === "ZIP_LIMIT"
|
||||
? "documents.zipLimit"
|
||||
: "documents.zipUnavailable"
|
||||
);
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
if (controller === request) busy.value = false;
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
controller?.abort();
|
||||
controller = undefined;
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user