nfp_sites/api/media/model.mjs

85 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
import path from 'path'
2019-09-14 19:03:38 +00:00
import bookshelf from '../bookshelf.mjs'
import config from '../config.mjs'
2019-02-20 16:10:37 +00:00
/*
Media model:
{
filename,
filetype,
small_image,
medium_image,
large_image,
*small_url,
*medium_url,
*large_url,
size,
staff_id,
is_deleted,
created_at,
updated_at,
}
*/
const Media = bookshelf.createModel({
tableName: 'media',
virtuals: {
small_url() {
return `${Media.baseUrl}${this.get('small_image')}`
},
medium_url() {
return `${Media.baseUrl}${this.get('medium_image')}`
},
large_url() {
return `${Media.baseUrl}${this.get('large_image')}`
},
2019-09-13 13:33:10 +00:00
2021-01-04 17:47:59 +00:00
small_url_avif() {
if (!this.get('small_image_avif')) return null
return `${Media.baseUrl}${this.get('small_image_avif')}`
},
medium_url_avif() {
if (!this.get('small_image_avif')) return null
return `${Media.baseUrl}${this.get('medium_image_avif')}`
},
large_url_avif() {
if (!this.get('small_image_avif')) return null
return `${Media.baseUrl}${this.get('large_image_avif')}`
},
2019-09-13 13:33:10 +00:00
link() {
return `${Media.baseUrl}${this.get('org_image')}`
2019-09-13 13:33:10 +00:00
},
url() {
2019-10-01 03:45:44 +00:00
return `${Media.baseUrl}${this.get('medium_image')}`
2019-09-13 13:33:10 +00:00
},
thumb() {
return `${Media.baseUrl}${this.get('small_image')}`
},
2019-02-20 16:10:37 +00:00
},
}, {
2019-09-14 19:03:38 +00:00
baseUrl: config.get('upload:baseurl'),
2019-02-20 16:10:37 +00:00
2021-01-04 17:47:59 +00:00
getSubUrl(input, size, type = 'jpg') {
2019-02-20 16:10:37 +00:00
if (!input) return input
let output = input
if (path.extname(input)) {
let ext = path.extname(input).toLowerCase()
output = input.slice(0, -ext.length)
}
2021-01-04 17:47:59 +00:00
return `${output}.${size}.${type}`
2019-02-20 16:10:37 +00:00
},
})
export default Media