diff --git a/src/core/Document.js b/src/core/Document.js index b62873b4..b7c423d2 100644 --- a/src/core/Document.js +++ b/src/core/Document.js @@ -21,7 +21,8 @@ class Document { constructor(data) { this.id = data.id this.hash = data.hash - this.file = new DocumentFile(data.file) + this.file = data.file + this.fileInfo = new DocumentFile(data.fileInfo) this.type = data.type this.title = data.title this.category = data.category diff --git a/src/routes/document.js b/src/routes/document.js index 47a6bda0..b86d327e 100644 --- a/src/routes/document.js +++ b/src/routes/document.js @@ -9,6 +9,7 @@ const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSl const {get} = require('koa-route') const {splitBy} = require('../lib/array') const {unique} = require('../utils/common') +const {bakeDocument} = require('./documents') const sizes = [ ImageArtifactType.MEDIUM, @@ -23,25 +24,6 @@ function getMeta(document) { } } -async function bakeDocument(document) { - //const preview = await document.preview.unpack() - const preview = document.preview - if (!preview) return null - - const image = preview.findArtifact(sizes) - if (!image) return null - - return { - category: document.category, - title: document.title, - fileName: document.file.name, - fileSize: document.file.size, - fileUrl: document.url, - imageUrl: image.url, - url: document.viewUrl, - } -} - function getDocument() { return get('/document/:id', async (ctx, id) => { const path = getPathWithNoTrailingSlash(ctx.path) @@ -60,7 +42,7 @@ function getDocument() { ) ctx.type = 'text/html' - ctx.body = await render(path, Component, getMeta(document)) + ctx.body = await render(path, Component, getMeta(document), {menuPadding: true}) } else { ctx.status = 404 } diff --git a/src/routes/documents.js b/src/routes/documents.js index 37a1e9b3..f65907cd 100644 --- a/src/routes/documents.js +++ b/src/routes/documents.js @@ -31,18 +31,23 @@ function getMeta(teacher) { } async function bakeDocument(document) { - //const preview = await document.preview.unpack() const preview = document.preview if (!preview) return null const image = preview.findArtifact(sizes) if (!image) return null + const category = (document.category || '') + .replace('Documents/', '') + .replace('Pages/', '') + .replace('Albums/', '') + .replace('Articles /', '') + return { - category: document.category, + category, title: document.title, - fileName: document.file.name, - fileSize: document.file.size, + fileName: document.fileInfo.name, + fileSize: document.fileInfo.size, fileUrl: document.url, imageUrl: image.url, url: document.viewUrl, @@ -50,16 +55,14 @@ async function bakeDocument(document) { } function getDocuments() { - return get('/documents', async (ctx) => { + return get('/documents', async ctx => { const path = getPathWithNoTrailingSlash(ctx.path) let documents = await Document.find({}) if (documents) { - //documents = teachersSorted(documents) documents = await Promise.all(documents.map(bakeDocument)) documents = documents.filter(Boolean) - //const collections = splitBy(d => d.category)(documents) const collections = getSorted(documents) const Component = ( @@ -152,4 +155,5 @@ function getSorted(documents) { }], []) } +exports.bakeDocument = bakeDocument exports.getDocuments = getDocuments