use file-type for mime and enhanced preview

and cleanup blob addition to text
This commit is contained in:
Henry 2020-10-12 16:34:19 +02:00
parent be412793c2
commit 5cb4c8aff1
3 changed files with 4292 additions and 2643 deletions

6891
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,7 @@
"@koa/router": "^8.0.0",
"debug": "^4.1.1",
"env-paths": "^2.2.0",
"file-type": "^15.0.1",
"highlight.js": "^9.18.1",
"hyperaxe": "^1.3.0",
"is-svg": "^4.2.1",
@ -41,6 +42,7 @@
"pull-sort": "^1.0.2",
"pull-stream": "^3.6.12",
"require-style": "^1.1.0",
"sharp": "^0.25.2",
"ssb-client": "^4.9.0",
"ssb-config": "^3.4.4",
"ssb-markdown": "^6.0.7",
@ -49,8 +51,7 @@
"ssb-ref": "^2.13.9",
"ssb-tangle": "^1.0.1",
"ssb-thread-schema": "^1.1.1",
"yargs": "^15.3.1",
"sharp": "^0.25.2"
"yargs": "^15.3.1"
},
"devDependencies": {
"@types/debug": "^4.1.5",

View File

@ -663,7 +663,7 @@ router
ctx.redirect(`/thread/${encodeURIComponent(message)}`);
})
.post("/publish/preview", koaBody({multipart: true }), async (ctx) => {
const text = String(ctx.request.body.text);
let text = String(ctx.request.body.text);
const rawContentWarning = String(ctx.request.body.contentWarning).trim();
const ssb = await cooler.open();
@ -673,22 +673,22 @@ router
image: await about.image(ssb.id),
}
let blobId = false
let blob = false
const blobUpload= ctx.request.files.blob
if (typeof blobUpload !== "undefined") {
const blob = await fs.promises.readFile(blobUpload.path);
if (blob.length > 0) {
const data = await fs.promises.readFile(blobUpload.path);
if (data.length > 0) {
// 5 MiB check
const mebibyte = Math.pow(2, 20);
const maxSize = 5 * mebibyte;
if (blob.length > maxSize) {
if (data.length > maxSize) {
throw new Error("Blob file is too big, maximum size is 5 mebibytes");
}
const addBlob = new Promise((resolve, reject) => {
pull(
pull.values([blob]),
pull.values([data]),
ssb.blobs.add((err, hashedBlobRef) => {
if (err) return reject(err);
console.log("added", hashedBlobRef)
@ -696,7 +696,30 @@ router
})
)
})
blobId = await addBlob;
blob = {
id: await addBlob,
name: blobUpload.name
}
const FileType = require('file-type');
try {
let ftype = await FileType.fromBuffer(data)
blob.mime = ftype.mime
} catch (error) {
console.warn(error)
blob.mime = "application/octet-stream"
}
}
}
// append uploaded blob as markdown to the end of the input text
if (typeof blob !== "boolean") {
if (blob.mime.startsWith("image/")) {
text += `\n![${blob.name}](${blob.id})`
} else if (blob.mime.startsWith("audio/")) {
text += `\n![audio:${blob.name}](${blob.id})`
} else if (blob.mime.startsWith("video/")) {
text += `\n![video:${blob.name}](${blob.id})`
} else {
text += `\n[${blob.name}](${blob.id})`
}
}
@ -704,7 +727,7 @@ router
const contentWarning =
rawContentWarning.length > 0 ? rawContentWarning : undefined;
ctx.body = await previewView({authorMeta, text, contentWarning, blobId});
ctx.body = await previewView({authorMeta, text, contentWarning});
})
.post("/publish/", koaBody(), async (ctx) => {
const text = String(ctx.request.body.text);