nfp_sites/api/file/model.mjs

46 lines
791 B
JavaScript
Raw Normal View History

2019-09-14 19:03:38 +00:00
import bookshelf from '../bookshelf.mjs'
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
}
*/
const File = bookshelf.createModel({
tableName: 'files',
2019-09-14 19:03:38 +00:00
virtuals: {
url() {
return `${File.baseUrl}${this.get('path')}`
},
magnet() {
let meta = this.get('meta')
if (!meta.torrent) return ''
return 'magnet:?'
+ 'xl=' + this.get('size')
+ '&dn=' + encodeURIComponent(meta.torrent.name)
+ '&xt=urn:btih:' + meta.torrent.hash
+ meta.torrent.announce.map(item => ('&tr=' + encodeURIComponent(item))).join('')
},
},
2019-09-13 13:33:10 +00:00
}, {
2019-09-14 19:03:38 +00:00
baseUrl: config.get('upload:baseurl'),
2019-09-13 13:33:10 +00:00
})
export default File