Skip to content

Commit

Permalink
✔ Attachment Upload & Original File Name ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Oct 10, 2023
1 parent 053f82e commit a2ef90f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/api/controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class AttachmentController {
}
const tempAttachment = this.tempAttachmentRepo.new();
tempAttachment.name = file.id;
tempAttachment.orig = file.originalName;
tempAttachment.ext = file.originalName.split('.').pop().toLowerCase();
tempAttachment.size = file.size;
tempAttachment.mime = file.metadata.mimeType;
Expand Down Expand Up @@ -255,7 +256,7 @@ export class AttachmentController {
res.setHeader('content-type', attachment.mime);
return res.download(
`${environment.uploadFolder}/${files[fIdx].name}`,
`${attachment.name}.${attachment.ext}`,
`${attachment.orig || attachment.name + '.' + attachment.ext}`,
async (e) => {
if (e) {
this.gs.log('[RES_DOWNLOAD_ATTACHMENT-ERROR] 🔻', e, 'error');
Expand Down
1 change: 1 addition & 0 deletions src/api/controllers/berkas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class BerkasController {
if (fIdx >= 0) {
const attachment = this.attachmentRepo.new();
attachment.name = tempAttachment.name;
attachment.orig = tempAttachment.orig;
attachment.size = tempAttachment.size;
attachment.ext = tempAttachment.ext;
attachment.mime = tempAttachment.mime;
Expand Down
3 changes: 3 additions & 0 deletions src/api/entities/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class Attachment implements AttachmentModel {
@Column({ type: 'text' })
name: string;

@Column({ type: 'text', nullable: true })
orig: string;

@Column({ type: 'text' })
ext: string;

Expand Down
3 changes: 3 additions & 0 deletions src/api/entities/TempAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class TempAttachment implements TempAttachmentModel {
@Column({ type: 'text' })
name: string;

@Column({ type: 'text' })
orig: string;

@Column({ type: 'text' })
ext: string;

Expand Down
100 changes: 63 additions & 37 deletions src/api/scheduler/upload-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,56 @@ export class UploadService {
//
}

async extractVideo(attachment: AttachmentModel) {
async uploadSubtitleAndFont(mkvAttachment: AttachmentModel) {
const files = readdirSync(`${environment.uploadFolder}`, { withFileTypes: true });
const fIdx = files.findIndex(f => f.name === mkvAttachment.name || f.name === `${mkvAttachment.name}.${mkvAttachment.ext}`);
if (fIdx >= 0) {
try {
const gdrive = await this.gdrive.gDrive(true);
const dfile = await gdrive.files.create({
requestBody: {
name: `${mkvAttachment.name}.${mkvAttachment.ext}`,
parents: [environment.gCloudPlatform.gDrive.folder_id],
mimeType: mkvAttachment.mime
},
media: {
mimeType: mkvAttachment.mime,
body: createReadStream(`${environment.uploadFolder}/${mkvAttachment.name}.${mkvAttachment.ext}`)
},
fields: 'id'
}, { signal: null });
const otherAttachment2 = await this.attachmentRepo.find({
where: [
{
name: Equal(mkvAttachment.name),
ext: Equal(mkvAttachment.ext),
google_drive: IsNull()
}
]
});
for (const oa of otherAttachment2) {
oa.google_drive = dfile.data.id;
oa.pending = false;
}
await this.attachmentRepo.save(otherAttachment2);
this.gs.deleteAttachment(`${mkvAttachment.name}.${mkvAttachment.ext}`);
} catch (e3) {
this.gs.log('[GDRIVE-ERROR] 💽', e3, 'error');
mkvAttachment.pending = false;
await this.attachmentRepo.save(mkvAttachment);
}
} else {
try {
mkvAttachment.pending = false;
const resSaveMkvAttachment = await this.attachmentRepo.save(mkvAttachment);
await this.attachmentRepo.remove(resSaveMkvAttachment as any);
} catch (e) {
this.gs.log('[FILE_ATTACHMENT-ERROR] 🎼', e, 'error');
}
}
}

async extractAndUploadVideoAndZip(attachment: AttachmentModel) {
const files = readdirSync(`${environment.uploadFolder}`, { withFileTypes: true });
const fIdx = files.findIndex(f => f.name === attachment.name || f.name === `${attachment.name}.${attachment.ext}`);
if (fIdx >= 0) {
Expand Down Expand Up @@ -102,40 +151,7 @@ export class UploadService {
const resMkvAttachmentSave = await this.attachmentRepo.save(mkvAttachment);
// Upload Video Attachment -- Subtitles, Fonts, etc
if (resMkvAttachmentSave.pending) {
try {
const gdrive = await this.gdrive.gDrive(true);
const dfile = await gdrive.files.create({
requestBody: {
name: `${resMkvAttachmentSave.name}.${resMkvAttachmentSave.ext}`,
parents: [environment.gCloudPlatform.gDrive.folder_id],
mimeType: resMkvAttachmentSave.mime
},
media: {
mimeType: resMkvAttachmentSave.mime,
body: createReadStream(`${environment.uploadFolder}/${fileName}.${fileExt}`)
},
fields: 'id'
}, { signal: null });
const otherAttachment2 = await this.attachmentRepo.find({
where: [
{
name: Equal(resMkvAttachmentSave.name),
ext: Equal(resMkvAttachmentSave.ext),
google_drive: IsNull()
}
]
});
for (const oa of otherAttachment2) {
oa.google_drive = dfile.data.id;
oa.pending = false;
}
await this.attachmentRepo.save(otherAttachment2);
this.gs.deleteAttachment(`${fileName}.${fileExt}`);
} catch (e3) {
this.gs.log('[GDRIVE-ERROR] 💽', e3, 'error');
resMkvAttachmentSave.pending = false;
await this.attachmentRepo.save(resMkvAttachmentSave);
}
await this.uploadSubtitleAndFont(resMkvAttachmentSave);
}
} catch (e2) {
this.gs.log('[FILE_ATTACHMENT-ERROR] 🎼', e2, 'error');
Expand Down Expand Up @@ -201,18 +217,28 @@ export class UploadService {
const attachments = await this.attachmentRepo.find({
where: [
{
ext: In(CONSTANTS.extAttachment),
ext: In([...CONSTANTS.extAttachment, ...CONSTANTS.extFonts, ...CONSTANTS.extSubs]),
pending: true
}
],
relations: ['user_']
});
for (const a of attachments) {
const isVideo = CONSTANTS.extAttachment.includes(a.ext);
const isFontSubs = [...CONSTANTS.extFonts, ...CONSTANTS.extSubs].includes(a.ext);
if (a.google_drive || a.discord) {
a.pending = false;
await this.attachmentRepo.save(a);
} else if (isVideo) {
await this.extractAndUploadVideoAndZip(a);
} else if (isFontSubs) {
await this.uploadSubtitleAndFont(a);
} else {
await this.extractVideo(a);
try {
await this.attachmentRepo.remove(a);
} catch (e) {
this.gs.log('[FILE_ATTACHMENT-ERROR] 🎼', e, 'error');
}
}
}
} catch (error) {
Expand Down
18 changes: 9 additions & 9 deletions src/app/_pages/berkas/berkas-detail/berkas-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ <h2 class="border-bottom-dotted">
<div class="col-12 text-center" *ngIf="isHaveDDL">
<div class="px-3">
<button type="button" class="m-1 w-100 shiny" mat-stroked-button color="accent" (click)="ddl(berkasData.attachment_.id)"
matTooltip="{{ lampiran.name }}.{{ lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan" *ngIf="!lampiran.data && !lampiran.isDownloading && !lampiran.isCompleted">
matTooltip="{{ lampiran.orig || lampiran.name + '.' + lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan" *ngIf="!lampiran.data && !lampiran.isDownloading && !lampiran.isCompleted">
<mat-icon fontIcon="movie" class="me-1"></mat-icon>
{{ lampiran.name | slice:0:5 }}.....{{ lampiran.ext }} ({{ lampiran.size | bytes }})
{{ lampiran.orig || lampiran.name | slice:0:5 }}.....{{ lampiran.ext }} ({{ lampiran.size | bytes }})
</button>
<button type="button" class="m-1 w-100" mat-stroked-button color="accent" (click)="cancel_dl(berkasData.attachment_.id)"
matTooltip="{{ lampiran.name }}.{{ lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan" *ngIf="!lampiran.data && lampiran.isDownloading">
matTooltip="{{ lampiran.orig || lampiran.name + '.' + lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan" *ngIf="!lampiran.data && lampiran.isDownloading">
<mat-icon fontIcon="close" class="me-1"></mat-icon>
Batal Unduh
</button>
<button type="button" class="m-1 w-100 shiny" mat-stroked-button color="accent" (click)="saveFileAs(berkasData.attachment_.id)" *ngIf="lampiran.data"
matTooltip="{{ lampiran.name }}.{{ lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan">
matTooltip="{{ lampiran.orig || lampiran.name + '.' + lampiran.ext }} :: {{ lampiran.download_count }}x Unduhan">
<mat-icon fontIcon="save" class="me-1"></mat-icon>
{{ lampiran.name | slice:0:5 }}.....{{ lampiran.ext }} ({{ lampiran.size | bytes }})
{{ lampiran.orig || lampiran.name | slice:0:5 }}.....{{ lampiran.ext }} ({{ lampiran.size | bytes }})
</button>
<div class="col">
<div class="px-3" *ngIf="lampiran.isDownloading && !lampiran.isCompleted">
Expand All @@ -102,14 +102,14 @@ <h2 class="border-bottom-dotted">
<hr class="my-3 border-bottom-dotted" style="height: 4px; background: url('/assets/img/stripe.png');" />
</div>
<button type="button" class="m-1 w-100" mat-stroked-button color="accent" *ngFor="let s of berkasData.attachment_.subtitles_"
(click)="standardDdlSubsFont(s.id)" matTooltip="{{ s.name }}.{{ s.name }} :: {{ s.download_count }}x Unduhan">
(click)="standardDdlSubsFont(s.id)" matTooltip="{{ s.orig || s.name + '.' + s.ext }} :: {{ s.download_count }}x Unduhan">
<mat-icon fontIcon="notes" class="me-1"></mat-icon>
{{ s.name | slice:0:5 }}.....{{ s.ext }} ({{ s.size | bytes }})
{{ s.orig || s.name | slice:0:5 }}.....{{ s.ext }} ({{ s.size | bytes }})
</button>
<button type="button" class="m-1 w-100" mat-stroked-button color="accent" *ngFor="let f of berkasData.attachment_.fonts_"
(click)="standardDdlSubsFont(f.id)" matTooltip="{{ f.name }}.{{ f.name }} :: {{ f.download_count }}x Unduhan">
(click)="standardDdlSubsFont(f.id)" matTooltip="{{ f.orig || f.name + '.' + f.ext }} :: {{ f.download_count }}x Unduhan">
<mat-icon fontIcon="font_download" class="me-1"></mat-icon>
{{ f.name | slice:0:5 }}.....{{ f.ext }} ({{ f.size | bytes }})
{{ f.orig || f.name | slice:0:5 }}.....{{ f.ext }} ({{ f.size | bytes }})
</button>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/app/_shared/services/download-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class DownloadManagerService {
if (!this.attachmentsDownload[attachment.id]) {
this.attachmentsDownload[attachment.id] = {};
this.attachmentsDownload[attachment.id].name = attachment.name;
this.attachmentsDownload[attachment.id].orig = attachment.orig;
this.attachmentsDownload[attachment.id].size = attachment.size;
this.attachmentsDownload[attachment.id].ext = attachment.ext;
this.attachmentsDownload[attachment.id].download_count = attachment.download_count;
Expand Down Expand Up @@ -186,7 +187,7 @@ export class DownloadManagerService {
saveFileAs(attachmentId): void {
this.gs.log('[SAVE_FILE]', attachmentId);
const attachment = this.attachmentsDownload[attachmentId];
saveAs(attachment.data, `${attachment.name}.${attachment.ext}`);
saveAs(attachment.data, `${attachment.orig || attachment.name + '.' + attachment.ext}`);
}

}
1 change: 1 addition & 0 deletions src/models/req-res.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface ApiKeyModel {
export interface AttachmentModel {
id?: string;
name?: string;
orig?: string;
ext?: string;
size?: number;
mime?: string;
Expand Down

0 comments on commit a2ef90f

Please sign in to comment.