Skip to content

Commit

Permalink
Fixed daysToKeepVideos being set on all channels by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Mar 25, 2023
1 parent a7f06aa commit bc29eb4
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export default class Channel {
public readonly title: ChannelOptions["title"];
public readonly identifiers: ChannelOptions["identifiers"];
public readonly skip: ChannelOptions["skip"];
public readonly daysToKeepVideos: ChannelOptions["daysToKeepVideos"];
public readonly daysToKeepVideos?: ChannelOptions["daysToKeepVideos"];

public readonly ignoreBeforeTimestamp: number;
public readonly ignoreBeforeTimestamp?: number;

public subscription: Subscription;

Expand All @@ -34,9 +34,10 @@ export default class Channel {
this.identifiers = channel.identifiers;
this.skip = channel.skip;

if (channel.daysToKeepVideos === undefined) channel.daysToKeepVideos = -1;
this.daysToKeepVideos = channel.daysToKeepVideos;
this.ignoreBeforeTimestamp = Date.now() - this.daysToKeepVideos * 24 * 60 * 60 * 1000;
if (channel.daysToKeepVideos !== undefined) {
this.daysToKeepVideos = channel.daysToKeepVideos;
this.ignoreBeforeTimestamp = Date.now() - this.daysToKeepVideos * 24 * 60 * 60 * 1000;
}

const databaseFilePath = `./db/channels/${subscription.creatorId}/${channel.title}.json`;
try {
Expand All @@ -47,15 +48,15 @@ export default class Channel {
}

public deleteOldVideos = async () => {
if (this.daysToKeepVideos !== -1) {
if (this.daysToKeepVideos !== undefined) {
process.stdout.write(
chalk`Checking for videos older than {cyanBright ${this.daysToKeepVideos}} days in channel {yellow ${this.title}} for {redBright deletion}...`
);
let deletedFiles = 0;
let deletedVideos = 0;
for (const video of Object.values(this._db.videos)) {
if (video.releaseDate === undefined || video.filePath === undefined) continue;
if (video.releaseDate < this.ignoreBeforeTimestamp) {
if (this.ignoreBeforeTimestamp !== undefined && video.releaseDate < this.ignoreBeforeTimestamp) {
deletedVideos++;
const deletionResults = await Promise.allSettled([
fs.rm(`${video.filePath}.mp4`),
Expand All @@ -81,7 +82,7 @@ export default class Channel {

public addVideo(video: BlogPost): Video | null {
const releaseDate = new Date(video.releaseDate).getTime();
if (this.daysToKeepVideos !== -1 && releaseDate < this.ignoreBeforeTimestamp) return null;
if (this.ignoreBeforeTimestamp !== undefined && releaseDate < this.ignoreBeforeTimestamp) return null;

// Set db info, have to instigate the db first before setting filepath
if (this._db.videos[video.guid] === undefined) {
Expand Down

0 comments on commit bc29eb4

Please sign in to comment.