761 lines
19 KiB
JavaScript
761 lines
19 KiB
JavaScript
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class DownloadsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const downloads = await db.downloads.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title || null,
|
|
|
|
download_type: data.download_type
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
progress_percent: data.progress_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
total_bytes: data.total_bytes
|
|
||
|
|
null
|
|
,
|
|
|
|
downloaded_bytes: data.downloaded_bytes
|
|
||
|
|
null
|
|
,
|
|
|
|
storage_path: data.storage_path
|
|
||
|
|
null
|
|
,
|
|
|
|
only_on_wifi: data.only_on_wifi
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
queued_at: data.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: data.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: data.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await downloads.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await downloads.setSeries( data.series || null, {
|
|
transaction,
|
|
});
|
|
|
|
await downloads.setChapter( data.chapter || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return downloads;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const downloadsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
title: item.title || null,
|
|
download_type: item.download_type
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
progress_percent: item.progress_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
total_bytes: item.total_bytes
|
|
||
|
|
null
|
|
,
|
|
|
|
downloaded_bytes: item.downloaded_bytes
|
|
||
|
|
null
|
|
,
|
|
|
|
storage_path: item.storage_path
|
|
||
|
|
null
|
|
,
|
|
|
|
only_on_wifi: item.only_on_wifi
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
queued_at: item.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: item.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: item.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const downloads = await db.downloads.bulkCreate(downloadsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return downloads;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const downloads = await db.downloads.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
if (data.download_type !== undefined) updatePayload.download_type = data.download_type;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.progress_percent !== undefined) updatePayload.progress_percent = data.progress_percent;
|
|
|
|
|
|
if (data.total_bytes !== undefined) updatePayload.total_bytes = data.total_bytes;
|
|
|
|
|
|
if (data.downloaded_bytes !== undefined) updatePayload.downloaded_bytes = data.downloaded_bytes;
|
|
|
|
|
|
if (data.storage_path !== undefined) updatePayload.storage_path = data.storage_path;
|
|
|
|
|
|
if (data.only_on_wifi !== undefined) updatePayload.only_on_wifi = data.only_on_wifi;
|
|
|
|
|
|
if (data.queued_at !== undefined) updatePayload.queued_at = data.queued_at;
|
|
|
|
|
|
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
|
|
|
|
|
if (data.finished_at !== undefined) updatePayload.finished_at = data.finished_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await downloads.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await downloads.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.series !== undefined) {
|
|
await downloads.setSeries(
|
|
|
|
data.series,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.chapter !== undefined) {
|
|
await downloads.setChapter(
|
|
|
|
data.chapter,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return downloads;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const downloads = await db.downloads.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of downloads) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of downloads) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return downloads;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const downloads = await db.downloads.findByPk(id, options);
|
|
|
|
await downloads.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await downloads.destroy({
|
|
transaction
|
|
});
|
|
|
|
return downloads;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const downloads = await db.downloads.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!downloads) {
|
|
return downloads;
|
|
}
|
|
|
|
const output = downloads.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await downloads.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.series = await downloads.getSeries({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.chapter = await downloads.getChapter({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'user',
|
|
|
|
where: filter.user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.series,
|
|
as: 'series',
|
|
|
|
where: filter.series ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.series.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.series.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.chapters,
|
|
as: 'chapter',
|
|
|
|
where: filter.chapter ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.chapter.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.chapter.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'downloads',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.storage_path) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'downloads',
|
|
'storage_path',
|
|
filter.storage_path,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.progress_percentRange) {
|
|
const [start, end] = filter.progress_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
progress_percent: {
|
|
...where.progress_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
progress_percent: {
|
|
...where.progress_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.total_bytesRange) {
|
|
const [start, end] = filter.total_bytesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_bytes: {
|
|
...where.total_bytes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_bytes: {
|
|
...where.total_bytes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.downloaded_bytesRange) {
|
|
const [start, end] = filter.downloaded_bytesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
downloaded_bytes: {
|
|
...where.downloaded_bytes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
downloaded_bytes: {
|
|
...where.downloaded_bytes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.queued_atRange) {
|
|
const [start, end] = filter.queued_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.started_atRange) {
|
|
const [start, end] = filter.started_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
started_at: {
|
|
...where.started_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
started_at: {
|
|
...where.started_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.finished_atRange) {
|
|
const [start, end] = filter.finished_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.download_type) {
|
|
where = {
|
|
...where,
|
|
download_type: filter.download_type,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.only_on_wifi) {
|
|
where = {
|
|
...where,
|
|
only_on_wifi: filter.only_on_wifi,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.downloads.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'downloads',
|
|
'title',
|
|
query,
|
|
),
|
|
Utils.ilike(
|
|
'downloads',
|
|
'status',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.downloads.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title || record.id,
|
|
}));
|
|
}
|
|
|
|
|
|
}; |