mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-04 00:51:29 +03:00
File deletion is now unified between sub and non-sub files
This commit is contained in:
@@ -1936,16 +1936,11 @@ components:
|
|||||||
description: Number of files removed
|
description: Number of files removed
|
||||||
DeleteSubscriptionFileRequest:
|
DeleteSubscriptionFileRequest:
|
||||||
required:
|
required:
|
||||||
- file
|
- file_uid
|
||||||
- sub
|
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
file:
|
|
||||||
type: string
|
|
||||||
file_uid:
|
file_uid:
|
||||||
type: string
|
type: string
|
||||||
sub:
|
|
||||||
$ref: '#/components/schemas/SubscriptionRequestData'
|
|
||||||
deleteForever:
|
deleteForever:
|
||||||
type: boolean
|
type: boolean
|
||||||
description: 'If true, does not remove id from archive. Only valid if youtube-dl archive is enabled in settings.'
|
description: 'If true, does not remove id from archive. Only valid if youtube-dl archive is enabled in settings.'
|
||||||
|
|||||||
@@ -1227,12 +1227,9 @@ app.post('/api/unsubscribe', optionalJwt, async (req, res) => {
|
|||||||
|
|
||||||
app.post('/api/deleteSubscriptionFile', optionalJwt, async (req, res) => {
|
app.post('/api/deleteSubscriptionFile', optionalJwt, async (req, res) => {
|
||||||
let deleteForever = req.body.deleteForever;
|
let deleteForever = req.body.deleteForever;
|
||||||
let file = req.body.file;
|
|
||||||
let file_uid = req.body.file_uid;
|
let file_uid = req.body.file_uid;
|
||||||
let sub = req.body.sub;
|
|
||||||
let user_uid = req.isAuthenticated() ? req.user.uid : null;
|
|
||||||
|
|
||||||
let success = await subscriptions_api.deleteSubscriptionFile(sub, file, deleteForever, file_uid, user_uid);
|
let success = await db_api.deleteFile(file_uid, deleteForever);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
res.send({
|
res.send({
|
||||||
@@ -1413,10 +1410,9 @@ app.post('/api/deletePlaylist', optionalJwt, async (req, res) => {
|
|||||||
app.post('/api/deleteFile', optionalJwt, async (req, res) => {
|
app.post('/api/deleteFile', optionalJwt, async (req, res) => {
|
||||||
const uid = req.body.uid;
|
const uid = req.body.uid;
|
||||||
const blacklistMode = req.body.blacklistMode;
|
const blacklistMode = req.body.blacklistMode;
|
||||||
const uuid = req.isAuthenticated() ? req.user.uid : null;
|
|
||||||
|
|
||||||
let wasDeleted = false;
|
let wasDeleted = false;
|
||||||
wasDeleted = await db_api.deleteFile(uid, uuid, blacklistMode);
|
wasDeleted = await db_api.deleteFile(uid, blacklistMode);
|
||||||
res.send(wasDeleted);
|
res.send(wasDeleted);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1448,7 +1444,7 @@ app.post('/api/deleteAllFiles', optionalJwt, async (req, res) => {
|
|||||||
|
|
||||||
for (let i = 0; i < files.length; i++) {
|
for (let i = 0; i < files.length; i++) {
|
||||||
let wasDeleted = false;
|
let wasDeleted = false;
|
||||||
wasDeleted = await db_api.deleteFile(files[i].uid, uuid, blacklistMode);
|
wasDeleted = await db_api.deleteFile(files[i].uid, blacklistMode);
|
||||||
if (wasDeleted) {
|
if (wasDeleted) {
|
||||||
delete_count++;
|
delete_count++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -451,8 +451,8 @@ exports.calculatePlaylistDuration = async (playlist, playlist_file_objs = null)
|
|||||||
return playlist_file_objs.reduce((a, b) => a + utils.durationStringToNumber(b.duration), 0);
|
return playlist_file_objs.reduce((a, b) => a + utils.durationStringToNumber(b.duration), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.deleteFile = async (uid, uuid = null, blacklistMode = false) => {
|
exports.deleteFile = async (uid, blacklistMode = false) => {
|
||||||
const file_obj = await exports.getVideo(uid, uuid);
|
const file_obj = await exports.getVideo(uid);
|
||||||
const type = file_obj.isAudio ? 'audio' : 'video';
|
const type = file_obj.isAudio ? 'audio' : 'video';
|
||||||
const folderPath = path.dirname(file_obj.path);
|
const folderPath = path.dirname(file_obj.path);
|
||||||
const ext = type === 'audio' ? 'mp3' : 'mp4';
|
const ext = type === 'audio' ? 'mp3' : 'mp4';
|
||||||
@@ -498,7 +498,7 @@ exports.deleteFile = async (uid, uuid = null, blacklistMode = false) => {
|
|||||||
|
|
||||||
let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive');
|
let useYoutubeDLArchive = config_api.getConfigItem('ytdl_use_youtubedl_archive');
|
||||||
if (useYoutubeDLArchive) {
|
if (useYoutubeDLArchive) {
|
||||||
const archive_path = utils.getArchiveFolder(type, uuid);
|
const archive_path = utils.getArchiveFolder(type, file_obj.user_uid, file_obj.sub_id ? (await exports.getRecord('subscriptions', {id: file_obj.sub_id})) : null);
|
||||||
|
|
||||||
// get ID from JSON
|
// get ID from JSON
|
||||||
|
|
||||||
|
|||||||
@@ -152,6 +152,10 @@ async function unsubscribe(sub, deleteMode, user_uid = null) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null, user_uid = null) {
|
async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null, user_uid = null) {
|
||||||
|
if (typeof sub === 'string') {
|
||||||
|
// TODO: fix bad workaround where sub is a sub_id
|
||||||
|
sub = await db_api.getRecord('subscriptions', {sub_id: sub});
|
||||||
|
}
|
||||||
// TODO: combine this with deletefile
|
// TODO: combine this with deletefile
|
||||||
let basePath = null;
|
let basePath = null;
|
||||||
basePath = user_uid ? path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions')
|
basePath = user_uid ? path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions')
|
||||||
|
|||||||
@@ -225,30 +225,24 @@ function deleteJSONFile(file_path, type) {
|
|||||||
async function removeIDFromArchive(archive_path, type, id) {
|
async function removeIDFromArchive(archive_path, type, id) {
|
||||||
const archive_file = path.join(archive_path, `archive_${type}.txt`);
|
const archive_file = path.join(archive_path, `archive_${type}.txt`);
|
||||||
const data = await fs.readFile(archive_file, {encoding: 'utf-8'});
|
const data = await fs.readFile(archive_file, {encoding: 'utf-8'});
|
||||||
if (!data) {
|
if (data === null || data === undefined) {
|
||||||
logger.error('Archive could not be found.');
|
logger.error('Archive could not be found.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dataArray = data.split('\n'); // convert file data in an array
|
let dataArray = data.split('\n'); // convert file data in an array
|
||||||
const searchKeyword = id; // we are looking for a line, contains, key word id in the file
|
const searchKeyword = id; // we are looking for a line, contains, key word id in the file
|
||||||
let lastIndex = -1; // let say, we have not found the keyword
|
let foundLine = dataArray.find(line => line.includes(searchKeyword));
|
||||||
|
|
||||||
for (let index=0; index<dataArray.length; index++) {
|
dataArray = dataArray.filter(line => !line.includes(searchKeyword));
|
||||||
if (dataArray[index].includes(searchKeyword)) { // check if a line contains the id keyword
|
|
||||||
lastIndex = index; // found a line includes a id keyword
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lastIndex === -1) return null;
|
if (!foundLine) return null;
|
||||||
|
else foundLine = foundLine.trim();
|
||||||
const line = dataArray.splice(lastIndex, 1); // remove the keyword id from the data Array
|
|
||||||
|
|
||||||
// UPDATE FILE WITH NEW DATA
|
// UPDATE FILE WITH NEW DATA
|
||||||
const updatedData = dataArray.join('\n');
|
const updatedData = dataArray.join('\n');
|
||||||
await fs.writeFile(archive_file, updatedData);
|
await fs.writeFile(archive_file, updatedData);
|
||||||
if (line) return Array.isArray(line) && line.length === 1 ? line[0] : line;
|
if (foundLine) return Array.isArray(foundLine) && foundLine.length === 1 ? foundLine[0] : foundLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeToBlacklist(archive_folder, type, line) {
|
async function writeToBlacklist(archive_folder, type, line) {
|
||||||
|
|||||||
@@ -2,12 +2,8 @@
|
|||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
|
||||||
import type { SubscriptionRequestData } from './SubscriptionRequestData';
|
|
||||||
|
|
||||||
export type DeleteSubscriptionFileRequest = {
|
export type DeleteSubscriptionFileRequest = {
|
||||||
file: string;
|
file_uid: string;
|
||||||
file_uid?: string;
|
|
||||||
sub: SubscriptionRequestData;
|
|
||||||
/**
|
/**
|
||||||
* If true, does not remove id from archive. Only valid if youtube-dl archive is enabled in settings.
|
* If true, does not remove id from archive. Only valid if youtube-dl archive is enabled in settings.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -316,16 +316,14 @@ export class RecentVideosComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deleteAndRedownload(file: DatabaseFile): void {
|
deleteAndRedownload(file: DatabaseFile): void {
|
||||||
const sub = this.postsService.getSubscriptionByID(file.sub_id);
|
this.postsService.deleteSubscriptionFile(file.uid, false).subscribe(() => {
|
||||||
this.postsService.deleteSubscriptionFile(sub, file.id, false, file.uid).subscribe(() => {
|
|
||||||
this.postsService.openSnackBar($localize`Successfully deleted file: ` + file.id);
|
this.postsService.openSnackBar($localize`Successfully deleted file: ` + file.id);
|
||||||
this.removeFileCard(file);
|
this.removeFileCard(file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteForever(file: DatabaseFile): void {
|
deleteForever(file: DatabaseFile): void {
|
||||||
const sub = this.postsService.getSubscriptionByID(file.sub_id);
|
this.postsService.deleteSubscriptionFile(file.uid, true).subscribe(() => {
|
||||||
this.postsService.deleteSubscriptionFile(sub, file.id, true, file.uid).subscribe(() => {
|
|
||||||
this.postsService.openSnackBar($localize`Successfully deleted file: ` + file.id);
|
this.postsService.openSnackBar($localize`Successfully deleted file: ` + file.id);
|
||||||
this.removeFileCard(file);
|
this.removeFileCard(file);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -552,9 +552,8 @@ export class PostsService implements CanActivate {
|
|||||||
return this.http.post<UnsubscribeResponse>(this.path + 'unsubscribe', body, this.httpOptions)
|
return this.http.post<UnsubscribeResponse>(this.path + 'unsubscribe', body, this.httpOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteSubscriptionFile(sub: SubscriptionRequestData, file: string, deleteForever: boolean, file_uid: string) {
|
deleteSubscriptionFile(file_uid: string, deleteForever: boolean) {
|
||||||
const body: DeleteSubscriptionFileRequest = {sub: sub, file: file, deleteForever: deleteForever,
|
const body: DeleteSubscriptionFileRequest = {file_uid: file_uid, deleteForever: deleteForever};
|
||||||
file_uid: file_uid};
|
|
||||||
return this.http.post<SuccessObject>(this.path + 'deleteSubscriptionFile', body, this.httpOptions)
|
return this.http.post<SuccessObject>(this.path + 'deleteSubscriptionFile', body, this.httpOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user