api image refactoring

- standart image definition for old preview and new cover
This commit is contained in:
tmshv committed 2020-03-11 13:36:15 +03:00
1 parent a3ffb00ed7
commit 877c63a874
11 files changed
+104 -188

No files matched your search

+10 -10
View File
@@ -11,8 +11,8 @@ export default class Document {
return Promise.all(items.map(processDocument))
}
static async findById(id) {
const data = await findOne(store(), { id })
static async findBySlug(slug: string) {
const data = await findOne(store(), { id: slug })
if (!data) return null
return processDocument(data)
@@ -21,22 +21,22 @@ export default class Document {
public id: any
public hash: any
public file: any
public fileInfo: any
public fileInfo: FileInfo
public type: any
public title: any
public category: any
public url: any
public preview: any
public preview: Image
public viewUrl: any
constructor(data) {
constructor(data: { preview: Image, [name: string]: any }) {
this.id = data.id
this.hash = data.hash
this.file = data.file
// In the future: use fileInfo only
this.fileInfo = data.fileInfo
? new DocumentFile(data.fileInfo)
: new DocumentFile(data.file)
? new FileInfo(data.fileInfo)
: new FileInfo(data.file)
this.type = data.type
this.title = data.title
this.category = data.category
@@ -47,9 +47,9 @@ export default class Document {
}
}
class DocumentFile {
public name: any
public size: any
class FileInfo {
public name: string
public size: number
constructor(data) {
this.name = data.name
+1 -40
View File
@@ -1,5 +1,4 @@
import Data from './Data'
import ImageArtifactType from './ImageArtifactType'
import { findOne } from '../lib/store'
import { ObjectId } from 'mongodb'
@@ -47,47 +46,9 @@ export default class Image {
this.artifacts = mapOf(ImageArtifact, artifacts)
}
getArtifact(size) {
getArtifact(size: string) {
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 {
@@ -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
-8
View File
@@ -20,14 +20,6 @@ export default class Page {
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) {
const item = await findOne(store(), { url })