Merge pull request #43 from tmshv/fix/api-image-simplify
API image simplify
This commit is contained in:
22 files changed
+170
-249
No files matched your search
@@ -1,35 +1,16 @@
|
|||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
import Document from '../core/Document'
|
import Document from '../core/Document'
|
||||||
import { encodeFile } from '../factory/file'
|
import { encodeFile, encodeFileUrl } from '../factory/file'
|
||||||
|
|
||||||
export async function getItem(req: Request, res: Response) {
|
export async function getItem(req: Request, res: Response) {
|
||||||
const id = req.params.slug
|
const slug = req.params.slug
|
||||||
const document = await Document.findById(id)
|
const item = await Document.findBySlug(slug)
|
||||||
|
if (item) {
|
||||||
if (document) {
|
res.json(encodeFile(item))
|
||||||
res.json(encodeFile(document))
|
|
||||||
} else {
|
} else {
|
||||||
res.status(404)
|
res.status(404)
|
||||||
res.json({
|
res.json({
|
||||||
error: `Resource ${id} not found`
|
error: `Resource ${slug} not found`
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getAll(req: Request, res: Response) {
|
|
||||||
const documents = await Document.find({ hidden: false })
|
|
||||||
if (documents) {
|
|
||||||
const items = documents
|
|
||||||
.map(encodeFile)
|
|
||||||
.filter(x => x.imageUrl) // skip documents without preview image; needs to be refactored
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
items,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
res.status(404)
|
|
||||||
res.json({
|
|
||||||
error: `Not found`
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,7 +19,7 @@ export async function getUrls(req: Request, res: Response) {
|
|||||||
const data = await Document.find({})
|
const data = await Document.find({})
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
const items = data.map(x => `/document/${x.id}`)
|
const items = data.map(encodeFileUrl)
|
||||||
res.json({
|
res.json({
|
||||||
items
|
items
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,30 @@
|
|||||||
import Image from '../core/Image'
|
import Image from '../core/Image'
|
||||||
import { encodeImage } from '../factory/image'
|
import { encodeImage } from '../factory/image'
|
||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
|
import { oid } from '../db'
|
||||||
|
|
||||||
export async function getItem(req: Request, res: Response) {
|
export async function getItem(req: Request, res: Response) {
|
||||||
|
const slug = req.params.slug
|
||||||
|
const id = oid(slug)
|
||||||
|
if (!id) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
error: `Failed to parse ${slug} as ObjectId`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const image = await Image.findById(id)
|
||||||
|
if (image) {
|
||||||
|
res.json(encodeImage(image))
|
||||||
|
} else {
|
||||||
|
res.status(404)
|
||||||
|
res.json({
|
||||||
|
error: `Resource ${slug} not found`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getItemByFile(req: Request, res: Response) {
|
||||||
const file = req.query.file
|
const file = req.query.file
|
||||||
const image = await Image.findByFile(file)
|
const image = await Image.findByFile(file)
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ export default class Document {
|
|||||||
return Promise.all(items.map(processDocument))
|
return Promise.all(items.map(processDocument))
|
||||||
}
|
}
|
||||||
|
|
||||||
static async findById(id) {
|
static async findBySlug(slug: string) {
|
||||||
const data = await findOne(store(), { id })
|
const data = await findOne(store(), { id: slug })
|
||||||
if (!data) return null
|
if (!data) return null
|
||||||
|
|
||||||
return processDocument(data)
|
return processDocument(data)
|
||||||
@@ -21,22 +21,22 @@ export default class Document {
|
|||||||
public id: any
|
public id: any
|
||||||
public hash: any
|
public hash: any
|
||||||
public file: any
|
public file: any
|
||||||
public fileInfo: any
|
public fileInfo: FileInfo
|
||||||
public type: any
|
public type: any
|
||||||
public title: any
|
public title: any
|
||||||
public category: any
|
public category: any
|
||||||
public url: any
|
public url: any
|
||||||
public preview: any
|
public preview: Image
|
||||||
public viewUrl: any
|
public viewUrl: any
|
||||||
|
|
||||||
constructor(data) {
|
constructor(data: { preview: Image, [name: string]: any }) {
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.hash = data.hash
|
this.hash = data.hash
|
||||||
this.file = data.file
|
this.file = data.file
|
||||||
// In the future: use fileInfo only
|
// In the future: use fileInfo only
|
||||||
this.fileInfo = data.fileInfo
|
this.fileInfo = data.fileInfo
|
||||||
? new DocumentFile(data.fileInfo)
|
? new FileInfo(data.fileInfo)
|
||||||
: new DocumentFile(data.file)
|
: new FileInfo(data.file)
|
||||||
this.type = data.type
|
this.type = data.type
|
||||||
this.title = data.title
|
this.title = data.title
|
||||||
this.category = data.category
|
this.category = data.category
|
||||||
@@ -47,9 +47,9 @@ export default class Document {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DocumentFile {
|
class FileInfo {
|
||||||
public name: any
|
public name: string
|
||||||
public size: any
|
public size: number
|
||||||
|
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import Data from './Data'
|
import Data from './Data'
|
||||||
import ImageArtifactType from './ImageArtifactType'
|
|
||||||
import { findOne } from '../lib/store'
|
import { findOne } from '../lib/store'
|
||||||
import { ObjectId } from 'mongodb'
|
import { ObjectId } from 'mongodb'
|
||||||
|
|
||||||
@@ -47,47 +46,9 @@ export default class Image {
|
|||||||
this.artifacts = mapOf(ImageArtifact, artifacts)
|
this.artifacts = mapOf(ImageArtifact, artifacts)
|
||||||
}
|
}
|
||||||
|
|
||||||
getArtifact(size) {
|
getArtifact(size: string) {
|
||||||
return this.artifacts.get(size)
|
return this.artifacts.get(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
findArtifact(sizes) {
|
|
||||||
for (let size of sizes) {
|
|
||||||
const a = this.getArtifact(size)
|
|
||||||
if (a) return a
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
getPicture(size) {
|
|
||||||
const retinaSize = ImageArtifactType.retina(size)
|
|
||||||
|
|
||||||
const a = this.getArtifact(size)
|
|
||||||
const a2 = this.getArtifact(retinaSize)
|
|
||||||
const srcset = !a2
|
|
||||||
? []
|
|
||||||
: [{
|
|
||||||
url: a2.url,
|
|
||||||
density: ImageArtifactType.RETINA_DENSITY,
|
|
||||||
}]
|
|
||||||
|
|
||||||
return {
|
|
||||||
src: a.url,
|
|
||||||
width: a.width,
|
|
||||||
height: a.height,
|
|
||||||
set: srcset,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getSrc(): string | null {
|
|
||||||
const a = this.getArtifact(ImageArtifactType.ORIGINAL)
|
|
||||||
if (!a) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.url
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ImageArtifact {
|
class ImageArtifact {
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
class ImageArtifactType {
|
|
||||||
constructor() {
|
|
||||||
throw new Error('Cannot create instance of ImageArtifactType')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageArtifactType.SMALL = 'small'
|
|
||||||
ImageArtifactType.MEDIUM = 'medium'
|
|
||||||
ImageArtifactType.BIG = 'big'
|
|
||||||
ImageArtifactType.LARGE = 'large'
|
|
||||||
ImageArtifactType.ORIGIN = 'original'
|
|
||||||
ImageArtifactType.ORIGINAL = 'original'
|
|
||||||
|
|
||||||
ImageArtifactType.FACEBOOK = 'fb'
|
|
||||||
|
|
||||||
ImageArtifactType.RETINA_DENSITY = 2
|
|
||||||
ImageArtifactType.retina = type => `${type}@${ImageArtifactType.RETINA_DENSITY}`
|
|
||||||
|
|
||||||
module.exports = ImageArtifactType
|
|
||||||
@@ -20,14 +20,6 @@ export default class Page {
|
|||||||
return Promise.all(items.map(processPage))
|
return Promise.all(items.map(processPage))
|
||||||
}
|
}
|
||||||
|
|
||||||
static async findById(id) {
|
|
||||||
const item = await findOne(store(), { id })
|
|
||||||
|
|
||||||
return !item
|
|
||||||
? null
|
|
||||||
: processPage(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
static async findByUrl(url: string) {
|
static async findByUrl(url: string) {
|
||||||
const item = await findOne(store(), { url })
|
const item = await findOne(store(), { url })
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,25 @@ export async function connect(dbName: string, mongoUri: string) {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
export function collection(name) {
|
export function collection(name: string) {
|
||||||
return db.collection(name)
|
return db.collection(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function id(i) {
|
export type ObjectIdLike = ObjectId | string
|
||||||
if (typeof i === 'string') return new ObjectId(i)
|
export function oid(i: ObjectIdLike | { _id: ObjectIdLike }): ObjectId | null {
|
||||||
else if (i instanceof ObjectId) return i
|
if (i instanceof ObjectId) {
|
||||||
else if (i instanceof Object) return '_id' in i ? new ObjectId(i._id) : null
|
return i
|
||||||
|
} else if (typeof i === 'string') {
|
||||||
|
try {
|
||||||
|
return new ObjectId(i)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
} else if (i instanceof Object) {
|
||||||
|
return '_id' in i ? oid(i._id) : null
|
||||||
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,35 +1,36 @@
|
|||||||
import ImageArtifactType from '../core/ImageArtifactType'
|
import Document from '../core/Document'
|
||||||
|
import { encodeImage, ImageDto } from './image'
|
||||||
|
|
||||||
const sizes = [
|
export type FileDto = {
|
||||||
ImageArtifactType.MEDIUM,
|
url: string
|
||||||
ImageArtifactType.BIG,
|
slug: string
|
||||||
ImageArtifactType.ORIGIN,
|
name: string
|
||||||
]
|
cover?: ImageDto
|
||||||
|
file: {
|
||||||
export function encodeFilePreview(file, sizes) {
|
name: string
|
||||||
const preview = file.preview
|
size: number
|
||||||
if (!preview) return null
|
type: string
|
||||||
|
src: string
|
||||||
const image = preview.findArtifact(sizes)
|
}
|
||||||
if (!image) return null
|
|
||||||
|
|
||||||
return image.url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeFile(file) {
|
export function encodeFile(file: Document): FileDto {
|
||||||
const imageUrl = encodeFilePreview(file, sizes)
|
const cover = file.preview ? encodeImage(file.preview) : null
|
||||||
const category = (file.category || '')
|
|
||||||
.replace('Documents/', '')
|
|
||||||
.replace('Pages/', '')
|
|
||||||
.replace('Articles /', '')
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
imageUrl,
|
url: encodeFileUrl(file),
|
||||||
category,
|
slug: file.id,
|
||||||
title: file.title,
|
cover,
|
||||||
fileName: file.fileInfo.name,
|
name: file.title,
|
||||||
fileSize: file.fileInfo.size,
|
file: {
|
||||||
fileUrl: file.url,
|
type: 'application/pdf',
|
||||||
url: file.viewUrl,
|
name: file.fileInfo.name,
|
||||||
|
size: file.fileInfo.size,
|
||||||
|
src: file.url,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encodeFileUrl(file: Document): string {
|
||||||
|
return `/document/${file.id}`
|
||||||
|
}
|
||||||
@@ -1,48 +1,22 @@
|
|||||||
import ImageArtifactType from '../core/ImageArtifactType'
|
|
||||||
import Image from '../core/Image'
|
import Image from '../core/Image'
|
||||||
|
|
||||||
export type ImageArtifactDto = {
|
const ORIGINAL = 'original'
|
||||||
height: number
|
|
||||||
width: number
|
|
||||||
src: string
|
|
||||||
set: Array<{
|
|
||||||
density: number
|
|
||||||
url: string
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ImageDto = {
|
export type ImageDto = {
|
||||||
artifacts: {
|
width: number
|
||||||
original: ImageArtifactDto,
|
height: number
|
||||||
large: ImageArtifactDto,
|
src: string
|
||||||
big: ImageArtifactDto,
|
|
||||||
medium: ImageArtifactDto,
|
|
||||||
small: ImageArtifactDto,
|
|
||||||
fb: ImageArtifactDto,
|
|
||||||
},
|
|
||||||
hash: string
|
|
||||||
file: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeImage(image: Image): ImageDto {
|
export function encodeImage(image: Image): ImageDto {
|
||||||
const artifacts = [
|
const a = image.getArtifact(ORIGINAL)
|
||||||
ImageArtifactType.SMALL,
|
if (!image) {
|
||||||
ImageArtifactType.MEDIUM,
|
return null
|
||||||
ImageArtifactType.BIG,
|
|
||||||
ImageArtifactType.LARGE,
|
|
||||||
ImageArtifactType.ORIGIN,
|
|
||||||
ImageArtifactType.ORIGINAL,
|
|
||||||
ImageArtifactType.FACEBOOK = 'fb'
|
|
||||||
].reduce((acc, size) => {
|
|
||||||
return {
|
|
||||||
...acc,
|
|
||||||
[size]: image.getPicture(size),
|
|
||||||
}
|
}
|
||||||
}, {})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
artifacts: artifacts as any,
|
src: a.url,
|
||||||
hash: image.hash,
|
width: a.width,
|
||||||
file: image.file,
|
height: a.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,29 +9,24 @@ type Token = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PageDto = {
|
type PageDto = {
|
||||||
id: string
|
|
||||||
title: string
|
title: string
|
||||||
date?: string
|
date?: string
|
||||||
url: string
|
url: string
|
||||||
data: string
|
data: string
|
||||||
description: string
|
description: string
|
||||||
preview?: ImageDto
|
cover?: ImageDto
|
||||||
tokens: Token[]
|
tokens: Token[]
|
||||||
tags: Tag[]
|
tags: Tag[]
|
||||||
featured: boolean
|
featured: boolean
|
||||||
breadcrumb: Breadcrumb
|
breadcrumb: Breadcrumb
|
||||||
coverSrc?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodePage(page: Page): PageDto {
|
export function encodePage(page: Page): PageDto {
|
||||||
const preview = page.preview ? encodeImage(page.preview) : null
|
const cover = page.preview ? encodeImage(page.preview) : null
|
||||||
const date = page.date ? page.date.toString() : null
|
const date = page.date ? page.date.toString() : null
|
||||||
const breadcrumb = page.getBreadcrumb()
|
const breadcrumb = page.getBreadcrumb()
|
||||||
|
|
||||||
const coverSrc = page.preview ? page.preview.getSrc() : null
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: page.id,
|
|
||||||
title: page.title,
|
title: page.title,
|
||||||
url: page.url,
|
url: page.url,
|
||||||
data: page.data,
|
data: page.data,
|
||||||
@@ -39,9 +34,8 @@ export function encodePage(page: Page): PageDto {
|
|||||||
tokens: page.tokens,
|
tokens: page.tokens,
|
||||||
tags: page.tags,
|
tags: page.tags,
|
||||||
featured: page.featured,
|
featured: page.featured,
|
||||||
preview,
|
cover,
|
||||||
date,
|
date,
|
||||||
breadcrumb,
|
breadcrumb,
|
||||||
coverSrc,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,6 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.get('/files', file.getAll)
|
|
||||||
app.get('/files/urls', file.getUrls)
|
app.get('/files/urls', file.getUrls)
|
||||||
app.get('/files/:slug', file.getItem)
|
app.get('/files/:slug', file.getItem)
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ image_sizes = [
|
|||||||
|
|
||||||
('large', 2500, 1000),
|
('large', 2500, 1000),
|
||||||
('large@2', 5000, 2000),
|
('large@2', 5000, 2000),
|
||||||
|
# ('large', 2500, 1000),
|
||||||
|
# ('large@2', 5000, 2000),
|
||||||
|
|
||||||
('big', 1500, 667),
|
('big', 1500, 667),
|
||||||
('big@2', 3000, 1334),
|
('big@2', 3000, 1334),
|
||||||
@@ -58,12 +60,18 @@ image_sizes = [
|
|||||||
|
|
||||||
('small', 250, 175),
|
('small', 250, 175),
|
||||||
('small@2', 500, 350),
|
('small@2', 500, 350),
|
||||||
|
# ('small', 250, 175),
|
||||||
|
# ('small@2', 500, 350),
|
||||||
|
|
||||||
('og', 968, 504),
|
('og', 968, 504),
|
||||||
('og@2', 1936, 1008),
|
('og@2', 1936, 1008),
|
||||||
|
# ('og', 968, 504),
|
||||||
|
# ('og@2', 1936, 1008),
|
||||||
|
|
||||||
('fb', 1200, 630),
|
('fb', 1200, 630),
|
||||||
('fb@2', 2400, 1260),
|
('fb@2', 2400, 1260),
|
||||||
|
# ('fb', 1200, 630),
|
||||||
|
# ('fb@2', 2400, 1260),
|
||||||
]
|
]
|
||||||
|
|
||||||
date_formats = [
|
date_formats = [
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ async def create_post(provider: Provider, folder: str, md: str, sizes):
|
|||||||
text = data['caption'] if data['caption'] else data['file']
|
text = data['caption'] if data['caption'] else data['file']
|
||||||
url = f'/document/{document.id}'
|
url = f'/document/{document.id}'
|
||||||
|
|
||||||
image_url = document.preview.get_size('small')['url']
|
image_url = document.preview.get_src()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'url': url,
|
'url': url,
|
||||||
|
|||||||
+1
-1
@@ -62,7 +62,7 @@ export const getStaticProps = async (ctx: any) => {
|
|||||||
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
||||||
const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb
|
const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb
|
||||||
const meta = (new MetaBuilder())
|
const meta = (new MetaBuilder())
|
||||||
.setImage(page.preview)
|
.setImage(page.cover)
|
||||||
.setTitle(page.title)
|
.setTitle(page.title)
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
@@ -109,15 +109,13 @@ export const getStaticProps = async (ctx: NextPageContext) => {
|
|||||||
const data: CardData[] = pages
|
const data: CardData[] = pages
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map(x => {
|
.map(x => {
|
||||||
const image = x.preview.artifacts.large
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: x.title,
|
title: x.title,
|
||||||
caption: x.description,
|
caption: x.description,
|
||||||
href: x.url,
|
href: x.url,
|
||||||
imageAlt: title,
|
imageAlt: title,
|
||||||
imageSrc: image.src,
|
imageSrc: x.cover.src,
|
||||||
imageSrcSet: imageSrcSet(image.src, [ImageSize.medium, ImageSize.small])
|
imageSrcSet: imageSrcSet(x.cover.src, [ImageSize.medium, ImageSize.small])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+14
-12
@@ -1,16 +1,15 @@
|
|||||||
import React from 'react'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from 'src/components/App'
|
import { App } from 'src/components/App'
|
||||||
import Document from 'src/components/Document'
|
import { File } from 'src/components/File'
|
||||||
import { Share } from 'src/components/Share'
|
import { Share } from 'src/components/Share'
|
||||||
import { meta } from 'src/lib/meta'
|
import { MetaBuilder } from 'src/lib/meta'
|
||||||
import { Meta } from 'src/components/Meta'
|
import { Meta } from 'src/components/Meta'
|
||||||
import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib'
|
import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib'
|
||||||
import { NextPage } from 'next'
|
import { NextPage } from 'next'
|
||||||
import { IMeta } from 'src/types'
|
import { IMeta, FileDefinition } from 'src/types'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
data: any //(file)
|
data: FileDefinition
|
||||||
meta: IMeta
|
meta: IMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,25 +25,28 @@ const Page: NextPage<Props> = props => {
|
|||||||
showAuthor={true}
|
showAuthor={true}
|
||||||
>
|
>
|
||||||
<Head>
|
<Head>
|
||||||
<title>{props.data.title}</title>
|
<title>{props.data.name}</title>
|
||||||
<Meta meta={props.meta} />
|
<Meta meta={props.meta} />
|
||||||
</Head>
|
</Head>
|
||||||
<Document {...props.data} />
|
<File {...props.data} />
|
||||||
<Share />
|
<Share />
|
||||||
</App>
|
</App>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getStaticProps = async (ctx: any) => {
|
export const getStaticProps = async (ctx: any) => {
|
||||||
const id = ctx.params.slug
|
const slug = ctx.params.slug
|
||||||
const data = await requestGet(createApiUrl(`/api/files/${id}`), {})
|
const data = await requestGet<FileDefinition>(createApiUrl(`/api/files/${slug}`), null)
|
||||||
|
const meta = (new MetaBuilder())
|
||||||
|
.setImage(data.cover)
|
||||||
|
.setTitle(data.name)
|
||||||
|
.setDescription(data.name)
|
||||||
|
.build()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
data,
|
data,
|
||||||
meta: meta({
|
meta,
|
||||||
// title: file.meta,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ const Index: NextPage<IProps> = props => {
|
|||||||
title: item.title,
|
title: item.title,
|
||||||
featured: item.featured,
|
featured: item.featured,
|
||||||
date: new Date(item.date),
|
date: new Date(item.date),
|
||||||
coverSrc: item.coverSrc,
|
coverSrc: item.cover?.src,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
const React = require('react')
|
|
||||||
const {ext, size} = require('../lib/file')
|
|
||||||
const {titleCase} = require('../lib/string')
|
|
||||||
|
|
||||||
const Document = ({title, url, imageUrl, fileUrl, fileName, fileSize}) => (
|
|
||||||
<div className="document">
|
|
||||||
<div className="document__image">
|
|
||||||
<a href={fileUrl} target="_blank" className="invisible">
|
|
||||||
<img src={imageUrl} alt={title}/>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="document__file">
|
|
||||||
<span>{titleCase(title)}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="document__file-info">
|
|
||||||
<a href={fileUrl} target="_blank">{ext(fileName)} ({size(fileSize)})</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
|
|
||||||
module.exports = Document
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { FileDefinition } from 'src/types'
|
||||||
|
import { ext, size } from 'src/lib/file'
|
||||||
|
import { titleCase } from 'src/lib/string'
|
||||||
|
|
||||||
|
export const File: React.SFC<FileDefinition> = props => (
|
||||||
|
<div className="document">
|
||||||
|
<h1>{titleCase(props.name)}</h1>
|
||||||
|
|
||||||
|
{!props.cover?.src ? null : (
|
||||||
|
<div className="document__image">
|
||||||
|
<a href={props.file.src} target="_blank" className="invisible">
|
||||||
|
<img src={props.cover.src} alt={props.name} />
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="document__file">
|
||||||
|
<span>{titleCase(props.name)}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="document__file-info">
|
||||||
|
<a href={props.file.src} target="_blank">{ext(props.file.name)} ({size(props.file.size)})</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
+3
-4
@@ -66,11 +66,10 @@ export class MetaBuilder {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const artifact = this.image.artifacts.fb
|
|
||||||
return {
|
return {
|
||||||
image: artifact.src,
|
image: this.image.src,
|
||||||
imageWidth: artifact.width,
|
imageWidth: this.image.width,
|
||||||
imageHeight: artifact.height,
|
imageHeight: this.image.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -27,7 +27,7 @@ export async function getPagesCardsByTags(tags: string[]): Promise<PageCardDto[]
|
|||||||
'title',
|
'title',
|
||||||
'featured',
|
'featured',
|
||||||
'date',
|
'date',
|
||||||
'coverSrc'
|
'cover'
|
||||||
]
|
]
|
||||||
|
|
||||||
const items = await getPagesByTags<PageCardDto>(tags, { fields, skip, limit, sortBy: '-date' })
|
const items = await getPagesByTags<PageCardDto>(tags, { fields, skip, limit, sortBy: '-date' })
|
||||||
|
|||||||
+16
-20
@@ -21,7 +21,20 @@ export type PageCardDto = {
|
|||||||
title: string
|
title: string
|
||||||
featured: boolean
|
featured: boolean
|
||||||
date: string
|
date: string
|
||||||
coverSrc: string
|
cover?: ImageDefinition
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FileDefinition = {
|
||||||
|
url: string
|
||||||
|
slug: string
|
||||||
|
name: string
|
||||||
|
cover?: ImageDefinition
|
||||||
|
file: {
|
||||||
|
name: string
|
||||||
|
size: string
|
||||||
|
type: string
|
||||||
|
src: string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPage {
|
export interface IPage {
|
||||||
@@ -30,7 +43,7 @@ export interface IPage {
|
|||||||
url: string
|
url: string
|
||||||
data: string
|
data: string
|
||||||
date: string
|
date: string
|
||||||
preview: ImageDefinition
|
cover: ImageDefinition
|
||||||
description: string
|
description: string
|
||||||
tokens: any[]
|
tokens: any[]
|
||||||
tags: ITag[]
|
tags: ITag[]
|
||||||
@@ -85,25 +98,8 @@ export enum ImageSize {
|
|||||||
thumbnail = 200,
|
thumbnail = 200,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ImageArtifact = {
|
export type ImageDefinition = {
|
||||||
width: number
|
width: number
|
||||||
height: number
|
height: number
|
||||||
src: string
|
src: string
|
||||||
set: Array<{
|
|
||||||
density: number
|
|
||||||
url: string
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ImageDefinition = {
|
|
||||||
file: string
|
|
||||||
hash: string
|
|
||||||
artifacts: {
|
|
||||||
original: ImageArtifact
|
|
||||||
large?: ImageArtifact
|
|
||||||
big?: ImageArtifact
|
|
||||||
medium?: ImageArtifact
|
|
||||||
small?: ImageArtifact
|
|
||||||
fb?: ImageArtifact
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user