This commit is contained in:
kngako
2022-06-19 03:00:48 +02:00
parent 95df947054
commit 4d02795a45
2 changed files with 41 additions and 10 deletions

View File

@@ -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()

View File

@@ -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