diff --git a/server/router/versions/index.js b/server/router/versions/index.js index dfb56c3..4fb119f 100644 --- a/server/router/versions/index.js +++ b/server/router/versions/index.js @@ -10,6 +10,20 @@ const characterCounter = (totalCharacterCount, chunk) => { return totalCharacterCount + chunk.length } +const saveFile = async (file, userId, chapterIndex, artifactVersion) => { + const fileText = file.data.toString() + // if (Object.hasOwnProperty.call(file, index)) { + // const file = request.files[index] + // } + return await saveChapter( + userId, + file.name, + fileText, + chapterIndex, + artifactVersion + ) +} + const saveChapter = async (userId, chapterName, text, chapterIndex, artifactVersion) => { const chunks = md.parse( text @@ -360,20 +374,31 @@ module.exports = function (options) { if (artifactVersion && request.files) { // TODO: Parse files and store their texts... const chapterIndex = artifactVersion.chapters.length + 1 - for (const index in request.files.chapters) { - const file = request.files.chapters[index] - const fileText = file.data.toString() - // if (Object.hasOwnProperty.call(file, index)) { - // const file = request.files[index] - // } - const chapter = await saveChapter( + if (request.files.chapters.length > 0) { + for (const index in request.files.chapters) { + const file = request.files.chapters[index] ?? request.files.chapters + const fileText = file.data.toString() + // if (Object.hasOwnProperty.call(file, index)) { + // const file = request.files[index] + // } + const chapter = await saveFile( + file, + request.user.id, + chapterIndex+index, + artifactVersion + ) + } + } else { + const file = request.files.chapters + + const chapter = await saveFile( + file, request.user.id, - file.name, - fileText, - chapterIndex+index, + chapterIndex, artifactVersion ) } + response.redirect(`/v/${artifactVersion.id}`) } else { next() diff --git a/server/views/artifact-version.pug b/server/views/artifact-version.pug index 90e7bef..921ded9 100644 --- a/server/views/artifact-version.pug +++ b/server/views/artifact-version.pug @@ -40,6 +40,12 @@ block content if isOwnedByUser .divider + .row + .col.s12 + p.flow-text + - + const wordCount = artifactVersion.chapters.reduce((totalWordCount, chapter) => totalWordCount+chapter.wordCount, 0) + span.chip #{wordCount.toLocaleString()} words .row .col.s12 a.btn.black(href=`/v/${artifactVersion.id}/chapters/add`) add chapter