Compare commits

..

2 Commits

Author SHA1 Message Date
Isaac Abadi
004a234b02 Downloads are now properly assigned a filename 2020-09-21 00:27:49 -04:00
Isaac Abadi
daca715d1b Fixes bug where playlists could not have download progress tracked
- downloads are now treated as playlists for cleaner logic
2020-09-20 23:01:43 -04:00
5 changed files with 43 additions and 32 deletions

View File

@@ -849,13 +849,12 @@ function getVideoFormatID(name)
}
}
async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvided = null, user_uid = null) {
async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvided = null) {
return new Promise(async resolve => {
let zipFolderPath = null;
if (!fullPathProvided) {
zipFolderPath = path.join(type === 'audio' ? audioFolderPath : videoFolderPath);
if (user_uid) zipFolderPath = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, zipFolderPath);
zipFolderPath = path.join(__dirname, (type === 'audio') ? audioFolderPath : videoFolderPath);
} else {
zipFolderPath = path.join(__dirname, config_api.getConfigItem('ytdl_subscriptions_base_path'));
}
@@ -880,7 +879,7 @@ async function createPlaylistZipFile(fileNames, type, outputName, fullPathProvid
for (let i = 0; i < fileNames.length; i++) {
let fileName = fileNames[i];
let fileNamePathRemoved = path.parse(fileName).base;
let file_path = !fullPathProvided ? path.join(zipFolderPath, fileName + ext) : fileName;
let file_path = !fullPathProvided ? zipFolderPath + fileName + ext : fileName;
archive.file(file_path, {name: fileNamePathRemoved + ext})
}
@@ -1160,7 +1159,12 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
return;
} else {
// store info in download for future use
download['_filename'] = info['_filename'];
if (Array.isArray(info)) {
download['fileNames'] = [];
for (let info_obj of info) download['fileNames'].push(info_obj['_filename']);
} else {
download['_filename'] = info['_filename'];
}
download['filesize'] = utils.getExpectedFileSize(info);
}
@@ -1614,12 +1618,15 @@ function checkDownloadPercent(download) {
Any file that starts with <video title> will be counted as part of the "bytes downloaded", which will
be divided by the "total expected bytes."
*/
const file_id = download['file_id'];
const filename = path.format(path.parse(download['_filename'].substring(0, download['_filename'].length-4)));
// assume it's a playlist for logic reasons
const fileNames = Array.isArray(download['fileNames']) ? download['fileNames']
: [path.format(path.parse(download['_filename'].substring(0, download['_filename'].length-4)))];
const resulting_file_size = download['filesize'];
glob(`${filename}*`, (err, files) => {
let sum_size = 0;
let sum_size = 0;
let glob_str = '';
glob(`{${fileNames.join(',')}, }*`, (err, files) => {
files.forEach(file => {
try {
const file_stats = fs.statSync(file);
@@ -1794,9 +1801,9 @@ const optionalJwt = function (req, res, next) {
const uuid = using_body ? req.body.uuid : req.query.uuid;
const uid = using_body ? req.body.uid : req.query.uid;
const type = using_body ? req.body.type : req.query.type;
const playlist_id = using_body ? req.body.id : req.query.id;
const file = !playlist_id ? auth_api.getUserVideo(uuid, uid, type, true, req.body) : auth_api.getUserPlaylist(uuid, playlist_id, null, false);
if (file) {
const file = !req.query.id ? auth_api.getUserVideo(uuid, uid, type, true, req.body) : auth_api.getUserPlaylist(uuid, req.query.id, null, true);
const is_shared = file ? file['sharingEnabled'] : false;
if (is_shared) {
req.can_watch = true;
return next();
} else {
@@ -2001,7 +2008,7 @@ app.post('/api/getAllFiles', optionalJwt, function (req, res) {
let audios = null;
let audio_playlists = null;
let video_playlists = null;
let subscriptions = config_api.getConfigItem('ytdl_allow_subscriptions') ? (subscriptions_api.getAllSubscriptions(req.isAuthenticated() ? req.user.uid : null)) : [];
let subscriptions = subscriptions_api.getAllSubscriptions(req.isAuthenticated() ? req.user.uid : null);
// get basic info depending on multi-user mode being enabled
if (req.isAuthenticated()) {
@@ -2549,8 +2556,7 @@ app.post('/api/downloadFile', optionalJwt, async (req, res) => {
for (let i = 0; i < fileNames.length; i++) {
fileNames[i] = decodeURIComponent(fileNames[i]);
}
file = await createPlaylistZipFile(fileNames, type, outputName, fullPathProvided, req.body.uuid);
file = path.join(__dirname, file);
file = await createPlaylistZipFile(fileNames, type, outputName, fullPathProvided);
}
res.sendFile(file, function (err) {
if (err) {

View File

@@ -283,7 +283,6 @@ exports.getUserVideos = function(user_uid, type) {
}
exports.getUserVideo = function(user_uid, file_uid, type, requireSharing = false) {
let file = null;
if (!type) {
file = users_db.get('users').find({uid: user_uid}).get(`files.audio`).find({uid: file_uid}).value();
if (!file) {

View File

@@ -105,19 +105,27 @@ function getDownloadedThumbnail(name, type, customPath = null) {
return null;
}
function getExpectedFileSize(info_json) {
if (info_json['filesize']) {
return info_json['filesize'];
}
function getExpectedFileSize(input_info_jsons) {
// treat single videos as arrays to have the file sizes checked/added to. makes the code cleaner
const info_jsons = Array.isArray(input_info_jsons) ? input_info_jsons : [input_info_jsons];
const formats = info_json['format_id'].split('+');
let expected_filesize = 0;
formats.forEach(format_id => {
info_json.formats.forEach(available_format => {
if (available_format.format_id === format_id && available_format.filesize) {
expected_filesize += available_format.filesize;
}
info_jsons.forEach(info_json => {
if (info_json['filesize']) {
expected_filesize += info_json['filesize'];
return;
}
const formats = info_json['format_id'].split('+');
let individual_expected_filesize = 0;
formats.forEach(format_id => {
info_json.formats.forEach(available_format => {
if (available_format.format_id === format_id && available_format.filesize) {
individual_expected_filesize += available_format.filesize;
}
});
});
expected_filesize += individual_expected_filesize;
});
return expected_filesize;

View File

@@ -317,8 +317,7 @@ export class PlayerComponent implements OnInit, AfterViewInit, OnDestroy {
const zipName = fileNames[0].split(' ')[0] + fileNames[1].split(' ')[0];
this.downloading = true;
this.postsService.downloadFileFromServer(fileNames, this.type, zipName, null, null, null, null,
!this.uuid ? this.postsService.user.uid : this.uuid, this.id).subscribe(res => {
this.postsService.downloadFileFromServer(fileNames, this.type, zipName).subscribe(res => {
this.downloading = false;
const blob: Blob = res;
saveAs(blob, zipName + '.zip');

View File

@@ -223,7 +223,7 @@ export class PostsService implements CanActivate {
}
downloadFileFromServer(fileName, type, outputName = null, fullPathProvided = null, subscriptionName = null, subPlaylist = null,
uid = null, uuid = null, id = null) {
uid = null, uuid = null) {
return this.http.post(this.path + 'downloadFile', {fileNames: fileName,
type: type,
zip_mode: Array.isArray(fileName),
@@ -232,8 +232,7 @@ export class PostsService implements CanActivate {
subscriptionName: subscriptionName,
subPlaylist: subPlaylist,
uuid: uuid,
uid: uid,
id: id
uid: uid
},
{responseType: 'blob', params: this.httpOptions.params});
}