Skip to content

Commit

Permalink
✔ Fix Duplicate Attachment ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Jul 7, 2023
1 parent 21de74f commit 65d0d25
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 76 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1688657467218,
"timestamp": 1688707161803,
"index": "/index.html",
"assetGroups": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

41 changes: 28 additions & 13 deletions src/api/controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ export class AttachmentController {
}
} else {
// Upload Video Attachment -- Subtitles, Fonts, etc
let otherAttachment = await this.attachmentRepo.find({
where: [
{
name: Equal(resAttachmentSave.name),
ext: Equal(resAttachmentSave.ext),
size: Equal(resAttachmentSave.size),
google_drive: IsNull()
}
]
});
if (otherAttachment.length > 0) {
for (const oa of otherAttachment) {
oa.pending = true;
}
otherAttachment = await this.attachmentRepo.save(otherAttachment);
}
this.gdrive.gDrive(true).then(async (gdrive) => {
const dfile = await gdrive.files.create({
requestBody: {
Expand All @@ -343,25 +359,24 @@ export class AttachmentController {
},
fields: 'id'
}, { signal: null });
const otherAttachment = await this.attachmentRepo.find({
where: [
{
name: Equal(resAttachmentSave.name),
ext: Equal(resAttachmentSave.ext),
google_drive: IsNull()
}
]
});
for (const oa of otherAttachment) {
oa.google_drive = dfile.data.id;
oa.pending = false;
if (otherAttachment.length > 0) {
for (const oa of otherAttachment) {
oa.google_drive = dfile.data.id;
oa.pending = false;
}
await this.attachmentRepo.save(otherAttachment);
}
await this.attachmentRepo.save(otherAttachment);
this.gs.deleteAttachment(files[fIdx].name);
}).catch(async (e5) => {
this.gs.log('[GDRIVE-ERROR] 💽', e5, 'error');
resAttachmentSave.pending = false;
await this.attachmentRepo.save(resAttachmentSave);
if (otherAttachment.length > 0) {
for (const oa of otherAttachment) {
oa.pending = false;
}
await this.attachmentRepo.save(otherAttachment);
}
});
}
}
Expand Down
151 changes: 92 additions & 59 deletions src/api/controllers/berkas.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// NodeJS Library
import { writeFile, createReadStream, readdirSync } from 'node:fs';
import { existsSync, writeFileSync, createReadStream, readdirSync } from 'node:fs';

import { Controller, Delete, Get, HttpCode, HttpException, HttpStatus, Post, Put, Req, Res } from '@nestjs/common';
import { ApiExcludeEndpoint, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
Expand Down Expand Up @@ -271,69 +271,102 @@ export class BerkasController {
const fileNameExt = ef.name.split('.');
const fileExt = fileNameExt.pop().toLowerCase();
const fileName = fileNameExt.join('.').toLowerCase();
writeFile(`${environment.uploadFolder}/${fileName}.${fileExt}`, ef.data, async (e2) => {
if (e2) {
this.gs.log('[NODE_FS_WRITE_FILE-ERROR] 📁', e2, 'error');
try {
const mkvAttachment = this.attachmentRepo.new();
mkvAttachment.name = fileName;
mkvAttachment.ext = fileExt;
mkvAttachment.size = ef.size;
mkvAttachment.pending = environment.production;
mkvAttachment.user_ = user;
mkvAttachment.parent_attachment_ = resAttachmentSave;
if (CONSTANTS.extSubs.includes(fileExt)) {
mkvAttachment.mime = 'text/plain';
} else if (CONSTANTS.extFonts.includes(fileExt)) {
mkvAttachment.mime = `font/${fileExt}`;
} else {
try {
const mkvAttachment = this.attachmentRepo.new();
mkvAttachment.name = fileName;
mkvAttachment.ext = fileExt;
mkvAttachment.size = ef.size;
mkvAttachment.user_ = user;
mkvAttachment.parent_attachment_ = resAttachmentSave;
if (CONSTANTS.extSubs.includes(fileExt)) {
mkvAttachment.mime = 'text/plain';
} else if (CONSTANTS.extFonts.includes(fileExt)) {
mkvAttachment.mime = `font/${fileExt}`;
} else {
mkvAttachment.mime = 'application/octet-stream';
mkvAttachment.mime = 'application/octet-stream';
}
let mkvAttachmentDuplicate = null;
const otherAttachment1 = await this.attachmentRepo.find({
where: [
{
name: Equal(fileName),
ext: Equal(fileExt),
size: Equal(ef.size)
}
const resMkvAttachmentSave = await this.attachmentRepo.save(mkvAttachment);
// Upload Video Attachment -- Subtitles, Fonts, etc
if (environment.production) {
this.gdrive.gDrive(true).then(async (gdrive) => {
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 otherAttachment = await this.attachmentRepo.find({
where: [
{
name: Equal(resMkvAttachmentSave.name),
ext: Equal(resMkvAttachmentSave.ext),
google_drive: IsNull()
}
]
});
for (const oa of otherAttachment) {
oa.google_drive = dfile.data.id;
oa.pending = false;
}
await this.attachmentRepo.save(otherAttachment);
this.gs.deleteAttachment(`${fileName}.${fileExt}`);
}).catch(async (e5) => {
this.gs.log('[GDRIVE-ERROR] 💽', e5, 'error');
resMkvAttachmentSave.pending = false;
await this.attachmentRepo.save(resMkvAttachmentSave);
});
} else {
resMkvAttachmentSave.pending = false;
await this.attachmentRepo.save(resMkvAttachmentSave);
]
});
if (otherAttachment1.length > 0) {
for (const oa of otherAttachment1) {
mkvAttachmentDuplicate = oa;
if (oa.google_drive) {
break;
}
}
}
const fileExist = existsSync(`${environment.uploadFolder}/${fileName}.${fileExt}`);
if (mkvAttachmentDuplicate) {
mkvAttachment.name = mkvAttachmentDuplicate.name;
mkvAttachment.ext = mkvAttachmentDuplicate.ext;
mkvAttachment.size = mkvAttachmentDuplicate.size;
mkvAttachment.mime = mkvAttachmentDuplicate.mime;
mkvAttachment.pending = false;
if (mkvAttachmentDuplicate.google_drive) {
mkvAttachment.google_drive = mkvAttachmentDuplicate.google_drive;
this.gs.deleteAttachment(`${fileName}.${fileExt}`);
} else {
// Local File Missing
if (!fileExist) {
writeFileSync(`${environment.uploadFolder}/${fileName}.${fileExt}`, ef.data);
}
} catch (e3) {
this.gs.log('[FILE_NOTE-ERROR] 🎼', e3, 'error');
}
} else {
// First Time Upload
if (!fileExist) {
writeFileSync(`${environment.uploadFolder}/${fileName}.${fileExt}`, ef.data);
}
}
const resMkvAttachmentSave = await this.attachmentRepo.save(mkvAttachment);
// Upload Video Attachment -- Subtitles, Fonts, etc
if (resMkvAttachmentSave.pending) {
this.gdrive.gDrive(true).then(async (gdrive) => {
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),
size: Equal(resMkvAttachmentSave.size),
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(async (e4) => {
this.gs.log('[GDRIVE-ERROR] 💽', e4, 'error');
resMkvAttachmentSave.pending = false;
await this.attachmentRepo.save(resMkvAttachmentSave);
});
}
});
} catch (e3) {
this.gs.log('[FILE_ATTACHMENT-ERROR] 🎼', e3, 'error');
}
}
}
videoExtractCompleted = true;
Expand Down
4 changes: 2 additions & 2 deletions src/api/services/global.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export class GlobalService {
}

// Only MKV Video Attachment Files Don't Have Any Extension -- Temporary Server Storage
deleteAttachment(videoFileNameNoExt: string) {
unlink(`${environment.uploadFolder}/${videoFileNameNoExt}`, (err) => {
deleteAttachment(fullFileName: string) {
unlink(`${environment.uploadFolder}/${fullFileName}`, (err) => {
if (err) {
this.log('[NODE_FS_UNLINK-ERROR] 🔗', err, 'error');
}
Expand Down

0 comments on commit 65d0d25

Please sign in to comment.