nfp_sites/old/file/model.mjs

60 lines
1.0 KiB
JavaScript
Raw Normal View History

2021-02-05 11:50:01 +00:00
import { createPrototype, safeColumns } from '../knex.mjs'
2019-09-14 19:03:38 +00:00
import config from '../config.mjs'
2019-09-13 13:33:10 +00:00
/*
File model:
{
filename,
filetype,
size,
2019-09-14 19:03:38 +00:00
path,
2019-09-13 13:33:10 +00:00
staff_id,
article_id,
is_deleted,
created_at,
updated_at,
2019-09-14 19:03:38 +00:00
*url,
*magnet,
2019-09-13 13:33:10 +00:00
}
*/
2021-02-05 11:50:01 +00:00
const baseUrl = config.get('upload:baseurl')
function FileItem(data) {
Object.assign(this, data)
this.url = `${baseUrl}${this.path}`
let meta = this.meta
if (!meta.torrent) {
this.magnet = ''
} else {
this.magnet = 'magnet:?'
+ 'xl=' + this.size
+ '&dn=' + encodeURIComponent(meta.torrent.name)
+ '&xt=urn:btih:' + meta.torrent.hash
+ meta.torrent.announce.map(item => ('&tr=' + encodeURIComponent(item))).join('')
}
}
function File() {
this.tableName = 'files'
this.Model = FileItem
this.publicFields = this.privateFields = safeColumns([
'article_id',
'filename',
'filetype',
'path',
'size',
'staff_id',
'meta',
])
this.init()
}
File.prototype = createPrototype({
2019-09-13 13:33:10 +00:00
})
2021-02-05 11:50:01 +00:00
export default new File()