diff --git a/package.json b/package.json index a8a462f..05e98d3 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "archives_manager", - "version": "2.0.4", + "version": "2.0.5", "description": "The file & metadata management app for archives.", "main": "./dist/main.bundle.js", "scripts": { @@ -33,7 +33,7 @@ "installerIcon": "./dist/public/archivesLogo.ico", "uninstallerIcon": "./dist/public/archivesLogo.ico" }, - "buildVersion": "2.0.4" + "buildVersion": "2.0.5" }, "author": "David Fernandes @dmf444", "license": "GPLv3", diff --git a/src/main/downloader/DefaultDownloader.ts b/src/main/downloader/DefaultDownloader.ts index 2da3fdb..0dc28c0 100644 --- a/src/main/downloader/DefaultDownloader.ts +++ b/src/main/downloader/DefaultDownloader.ts @@ -33,4 +33,8 @@ export class DefaultDownloader implements IDownloader { }; } + createdFilePostback(file): void { + + } + } \ No newline at end of file diff --git a/src/main/downloader/FileManager.ts b/src/main/downloader/FileManager.ts index 1f759ca..e80bbd2 100644 --- a/src/main/downloader/FileManager.ts +++ b/src/main/downloader/FileManager.ts @@ -38,7 +38,8 @@ export class FileManager { } FileUtils.queryForDuplicates(hashCheck).then((contains: boolean) => { - FileUtils.createNewFileEntry(filePath, fileName, url, contains, hashCheck, stage); + let file = FileUtils.createNewFileEntry(filePath, fileName, url, contains, hashCheck, stage); + downloader.createdFilePostback(file); sendSuccess("Download Success!", `File ${fileName} was downloaded successfully!`); }); diff --git a/src/main/downloader/IDownloader.ts b/src/main/downloader/IDownloader.ts index cd5b93d..94e5821 100644 --- a/src/main/downloader/IDownloader.ts +++ b/src/main/downloader/IDownloader.ts @@ -21,4 +21,9 @@ interface IDownloader { downloadUrl(url: string, stage: boolean): Promise; + /** + * + * @param file FileModel the created file, for allowing changes to data. + */ + createdFilePostback(file): void; } \ No newline at end of file diff --git a/src/main/downloader/YouTubeDownloader.ts b/src/main/downloader/YouTubeDownloader.ts index b651f10..c8d8750 100644 --- a/src/main/downloader/YouTubeDownloader.ts +++ b/src/main/downloader/YouTubeDownloader.ts @@ -2,6 +2,8 @@ import * as jetpack from "fs-jetpack"; import {FileUtils} from "@main/downloader/FileUtils"; import {YtdlBuilder} from "@main/youtubedl/YtdlBuilder"; import {InspectResult} from "fs-jetpack/types"; +import {FileModel} from "@main/file/FileModel"; +import {getFileDatabase} from "@main/main"; const path = require('path'); const log = require('electron-log'); const AdmZip = require('adm-zip'); @@ -27,20 +29,11 @@ export class YouTubeDownloader implements IDownloader { let downloadPromise: downloadPromise; if (responseCode == 0) { let fileNames: string[] = jetpack.list(initalDirectory); + let videoFileName: string = this.getVideoFileName(fileNames); + let md5: string = this.getMd5FromFile(initalDirectory + path.sep + videoFileName); - let fileName: string = this.getVideoFileName(fileNames); - let md5: string = this.getMd5FromFile(initalDirectory + path.sep + fileName); + let [zipName, zipBasePath] = this.zipFiles(fileNames, videoFileName, initalDirectory, stage); - var zip = new AdmZip(); - fileNames.forEach((dlFileName: string) => { - zip.addLocalFile(initalDirectory + path.sep + dlFileName); - }); - - let zipBasePath: string = FileUtils.getFilePath(stage); - let lastDot: number = fileName.lastIndexOf("."); - let zipName: string = fileName.substring(0, lastDot) + ".zip"; - let zipSavePath: string = zipBasePath + path.sep + zipName; - zip.writeZip(zipSavePath); log.info("Video Downloaded!"); downloadPromise = {state: "completed", fileName: zipName, filePathDir: zipBasePath, md5: md5}; @@ -78,4 +71,60 @@ export class YouTubeDownloader implements IDownloader { return initalDirectory; } + private zipFiles(fileNames: string[], videoFileName: string, initalDirectory: string, stage: boolean) { + var zip = new AdmZip(); + fileNames.forEach((dlFileName: string) => { + zip.addLocalFile(initalDirectory + path.sep + dlFileName); + }); + + let zipBasePath: string = FileUtils.getFilePath(stage); + let lastDot: number = videoFileName.lastIndexOf("."); + let zipName: string = videoFileName.substring(0, lastDot) + ".zip"; + let zipSavePath: string = zipBasePath + path.sep + zipName; + zip.writeZip(zipSavePath); + return [zipName, zipBasePath]; + } + + private getJsonFileFromZip(fileLocation: string): boolean { + let zip = new AdmZip(fileLocation); + let entries = zip.getEntries(); + let searchEntry = null; + entries.forEach(zipEntry => { + if(zipEntry.name.endsWith(".json")) { + searchEntry = zipEntry; + } + }); + if(searchEntry !== null) { + zip.extractEntryTo(searchEntry.entryName, FileUtils.getFilePath(true), false, true); + return searchEntry.name; + } + return false; + } + + public createdFilePostback(file: FileModel): void { + let zipName = this.getJsonFileFromZip(file.savedLocation); + log.info(zipName); + if(zipName !== false){ + let jsonFilePath = FileUtils.getFilePath(true) + zipName; + let videoJson = jetpack.read(jsonFilePath, 'json'); + + let archivesJson = { + "original_url": videoJson['webpage_url'], + "description": "", + "copyright": "", + "upload_date": videoJson['upload_date'], + "video_description": videoJson['description'], + "channel_name": videoJson['channel'], + "channel_url": videoJson['uploader_url'], + "original_tags": videoJson['tags'] + }; + let description = JSON.stringify(archivesJson); + file.fileMetadata.descriptionVersion = "2.1.0"; + file.fileMetadata.description = description; + getFileDatabase().updateFile(file); + + jetpack.remove(jsonFilePath); + } + } + } \ No newline at end of file diff --git a/src/main/youtubedl/YtdlBuilder.ts b/src/main/youtubedl/YtdlBuilder.ts index f9b1aef..9b792b9 100644 --- a/src/main/youtubedl/YtdlBuilder.ts +++ b/src/main/youtubedl/YtdlBuilder.ts @@ -82,6 +82,11 @@ export class YtdlBuilder { return this; } + public rencodeToMp4(): YtdlBuilder { + this.executeArgs.push('--recode-video mp4'); + return this; + } + //Authentication options. No intent to use them, but they're here; just in case! public authenticated(username: string, password: string): YtdlBuilder {