Skip to content

Commit

Permalink
✔ Fix Expiry Link ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Oct 25, 2023
1 parent 14c7f18 commit a124df5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 13 deletions.
37 changes: 33 additions & 4 deletions src/api/controllers/ddl-file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DdlFileService } from '../repository/ddl-file';

import { ApiService } from '../services/api.service';
import { GlobalService } from '../services/global.service';
import { DiscordService } from '../services/discord.service';

@Controller('/ddl-part')
export class DdlPartController {
Expand All @@ -26,7 +27,8 @@ export class DdlPartController {
private api: ApiService,
private attachmentRepo: AttachmentService,
private gs: GlobalService,
private ddlFileRepo: DdlFileService
private ddlFileRepo: DdlFileService,
private ds: DiscordService
) {
//
}
Expand All @@ -39,11 +41,24 @@ export class DdlPartController {
@Roles(RoleModel.ADMIN, RoleModel.MODERATOR, RoleModel.FANSUBBER, RoleModel.USER)
async downloadChunk(@Req() req: Request, @Res( /* { passthrough: true } */ ) res: Response): Promise<any> {
try {
const ddlFile = await this.ddlFileRepo.findOneOrFail({
let ddlFile = await this.ddlFileRepo.findOneOrFail({
where: [
{ id: Equal(req.params['id']) }
]
});
const msg = await this.ds.getMessageAttachment(ddlFile.msg_id);
const att = msg.attachments.get(ddlFile.id);
let newUrl = att.url;
if (newUrl.endsWith('/?')) {
newUrl = newUrl.substring(0, newUrl.length - 2);
}
if (newUrl.endsWith('&') || newUrl.endsWith('/') || newUrl.endsWith('?')) {
newUrl = newUrl.substring(0, newUrl.length - 1);
}
if (ddlFile.url !== newUrl) {
ddlFile.url = newUrl;
ddlFile = await this.ddlFileRepo.save(ddlFile);
}
const res_raw = await this.api.getData(
new URL(ddlFile.url),
{
Expand Down Expand Up @@ -95,7 +110,8 @@ export class DdlSeekController {
private api: ApiService,
private attachmentRepo: AttachmentService,
private gs: GlobalService,
private ddlFileRepo: DdlFileService
private ddlFileRepo: DdlFileService,
private ds: DiscordService
) {
//
}
Expand Down Expand Up @@ -148,11 +164,24 @@ export class DdlSeekController {
chunkSize += df.size;
chunkIdx++;
}
const ddlFile = ddlFiles[chunkIdx];
let ddlFile = ddlFiles[chunkIdx];
let hdrRngPrxy = `bytes=${skippedChunkSize}-`;
if (headerRangeStartEnd.length > 1 && headerRangeStartEnd[1]) {
hdrRngPrxy += `${parseInt(headerRangeStartEnd[1], 10) - target + skippedChunkSize}`;
}
const msg = await this.ds.getMessageAttachment(ddlFile.msg_id);
const att = msg.attachments.get(ddlFile.id);
let newUrl = att.url;
if (newUrl.endsWith('/?')) {
newUrl = newUrl.substring(0, newUrl.length - 2);
}
if (newUrl.endsWith('&') || newUrl.endsWith('/') || newUrl.endsWith('?')) {
newUrl = newUrl.substring(0, newUrl.length - 1);
}
if (ddlFile.url !== newUrl) {
ddlFile.url = newUrl;
ddlFile = await this.ddlFileRepo.save(ddlFile);
}
const res_raw = await this.api.getData(
new URL(ddlFile.url),
{
Expand Down
75 changes: 66 additions & 9 deletions src/api/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class DiscordService {
this.gs.log(`[DISCORD_SERVICE-READY] 🎉 ${this.bot.user.username}#${this.bot.user.discriminator} - ${this.bot.user.id} 🎶`);
if (cluster.isMaster) {
await this.changeBotNickname();
// await this.recoverAndFindMessageByAttachmentCrawl();
}
} catch (error) {
this.gs.log('[DISCORD_SERVICE-FAILED] 🎉', error, 'error');
Expand Down Expand Up @@ -146,15 +147,66 @@ export class DiscordService {
}
}

// async recoverAndFindMessageByAttachmentCrawl(): Promise<void> {
// try {
// const ddl = await this.ddlFileRepo.query(`
// SELECT msg_id, max(chunk_idx) AS max
// FROM public.ddl_file
// WHERE msg_id IN (
// SELECT DISTINCT msg_id
// FROM public.ddl_file
// WHERE msg_id = msg_parent
// )
// GROUP BY msg_id
// `);
// for (const d of ddl) {
// let lastMessageId = d.msg_id;
// for (let i = 0; i < d.max; i++) {
// const chnl = await this.bot.channels.fetch(environment.discord.channelDdlId);
// const res = await (chnl as NewsChannel).messages.fetch({ after: lastMessageId, limit: 1 });
// const kys = res.keys();
// for (const k of kys) {
// const msg = res.get(k);
// lastMessageId = msg.id;
// const att = msg.attachments?.first();
// if (att) {
// this.gs.log('[DISCORD_SERVICE-ATTACHMMENT_MESSAGE_CRAWLER] 🎉', `${att.id} => ${att.name} :: ${msg.id}`);
// const dfrs = await this.ddlFileRepo.find({
// where: [
// { id: Equal(att.id) }
// ]
// });
// if (dfrs.length === 1) {
// const dfr = dfrs[0];
// if (dfr.msg_id !== msg.id) {
// dfr.msg_id = msg.id;
// await this.ddlFileRepo.save(dfr);
// }
// }
// }
// }
// }
// }
// } catch (error) {
// this.gs.log(`[DISCORD_SERVICE-ATTACHMMENT_MESSAGE_CRAWLER] 🎉`, error, 'error');
// }
// }

async getMessageAttachment(msg_id: string): Promise<Message<boolean>> {
const botDdlChannel = this.bot ? (this.bot.channels.cache.get(environment.discord.channelDdlId) as NewsChannel) : null;
if (botDdlChannel) {
// Fore Re-Fresh `.fetch()`
return await botDdlChannel.messages.fetch(msg_id);
}
return null;
}

async deleteAttachment(msg_ids: string[]): Promise<void> {
try {
const botDdlChannel = this.bot ? (this.bot.channels.cache.get(environment.discord.channelDdlId) as NewsChannel) : null;
if (botDdlChannel) {
for (const msg_id of msg_ids) {
const botMessage = botDdlChannel.messages.cache.get(msg_id);
if (botMessage) {
await botMessage.delete();
}
for (const msg_id of msg_ids) {
const botMessage = await this.getMessageAttachment(msg_id);
if (botMessage) {
await botMessage.delete();
}
}
} catch (error) {
Expand All @@ -166,7 +218,8 @@ export class DiscordService {
let currentChunkIdx: number = 0;
let chunkParent: string = attachment?.discord;
let chunkSize = CONSTANTS.fileSizeAttachmentChunkDiscordLimit;
const botGuild = this.bot ? this.bot.guilds.cache.get(environment.discord.guild_id) : null;
// Fore Re-Fresh `.fetch()`
const botGuild = this.bot ? await this.bot.guilds.fetch(environment.discord.guild_id) : null;
if (botGuild) {
const totalBoosts = botGuild.premiumSubscriptionCount;
if (totalBoosts >= 14) {
Expand Down Expand Up @@ -221,7 +274,11 @@ export class DiscordService {
ddlFile.user_ = attachment.user_ || user;
ddlFile.id = msg.attachments.first().id;
ddlFile.name = msg.attachments.first().name;
ddlFile.url = msg.attachments.first().url.split('?')[0];
let newUrl = msg.attachments.first().url;
if (newUrl.endsWith('&') || newUrl.endsWith('/')) {
newUrl = newUrl.substring(0, -1);
}
ddlFile.url = newUrl;
ddlFile.size = msg.attachments.first().size;
ddlFile.mime = attachment.mime;
await this.ddlFileRepo.save(ddlFile);
Expand Down

0 comments on commit a124df5

Please sign in to comment.