fix race on blobs variable

fixes TypeError: Cannot read property 'startsWith' of undefined
This commit is contained in:
Henry 2020-11-01 14:39:39 +01:00
parent 4d5c571df3
commit e3906e2230
1 changed files with 13 additions and 14 deletions

View File

@ -221,7 +221,6 @@ const preparePreview = async function (ctx) {
// finally it returns the correct markdown link for the blob depending on the mime-type.
// it supports plain, image and also audio: and video: as understood by ssbMarkdown.
const handleBlobUpload = async function (ctx) {
let hasBlob = false;
let text = "";
if (!ctx.request.files) return "";
@ -275,7 +274,8 @@ const handleBlobUpload = async function (ctx) {
id: await addBlob,
name: blobUpload.name,
};
hasBlob = true;
// determain encoding to add the correct markdown link
const FileType = require("file-type");
try {
let ftype = await FileType.fromBuffer(data);
@ -284,18 +284,17 @@ const handleBlobUpload = async function (ctx) {
console.warn(error);
blob.mime = "application/octet-stream";
}
}
}
// append uploaded blob as markdown to the end of the input text
if (hasBlob) {
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})`;
// append uploaded blob as markdown to the end of the input text
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})`;
}
}
}
return text;