-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract media conversion into adapter pattern
- Loading branch information
1 parent
e339539
commit b7bf466
Showing
31 changed files
with
740 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-env node */ | ||
|
||
const { defineConfig } = require('eslint-define-config'); | ||
const prettierConfig = require('./.prettierrc.js'); | ||
|
||
module.exports = defineConfig({ | ||
root: true, | ||
env: { | ||
browser: true, | ||
es6: true, | ||
node: true, | ||
jest: true, | ||
}, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/eslint-recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:import/recommended', | ||
'plugin:import/electron', | ||
'plugin:import/typescript', | ||
'prettier', | ||
'@vue/eslint-config-typescript', | ||
'plugin:vue/vue3-essential', | ||
'plugin:vuejs-accessibility/recommended', | ||
], | ||
plugins: ['prettier', 'vuejs-accessibility', '@typescript-eslint'], | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { | ||
parser: '@typescript-eslint/parser', | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
}, | ||
rules: { | ||
indent: [ | ||
'error', | ||
2, | ||
{ | ||
SwitchCase: 1, | ||
}, | ||
], | ||
'max-len': [ | ||
'error', | ||
{ | ||
code: 120, | ||
}, | ||
], | ||
'no-console': [ | ||
'error', | ||
{ | ||
allow: ['warn', 'error'], | ||
}, | ||
], | ||
'comma-dangle': ['error', 'always-multiline'], | ||
'space-before-function-paren': [ | ||
'warn', | ||
{ | ||
anonymous: 'ignore', | ||
named: 'never', | ||
asyncArrow: 'always', | ||
}, | ||
], | ||
'prettier/prettier': [ | ||
'error', | ||
{ | ||
...prettierConfig, | ||
}, | ||
], | ||
'vue/html-indent': ['error', 2], | ||
'vue/multiline-html-element-content-newline': 'off', | ||
'vue/multi-word-component-names': 'off', | ||
'vue/max-attributes-per-line': 0, | ||
'vue/require-default-prop': 0, | ||
'vue/no-multiple-template-root': 0, | ||
'@typescript-eslint/no-unused-vars': [ | ||
'warn', | ||
{ | ||
varsIgnorePattern: '^_$', | ||
argsIgnorePattern: '^_$', | ||
}, | ||
], | ||
}, | ||
settings: { | ||
'import/resolver': { | ||
typescript: { | ||
alwaysTryTypes: true, | ||
project: './tsconfig.json', | ||
}, | ||
}, | ||
}, | ||
ignorePatterns: ['*.test.ts'], | ||
overrides: [ | ||
{ | ||
files: ['tests/**/*'], | ||
env: { | ||
jest: true, | ||
}, | ||
}, | ||
{ | ||
files: ['*.vue'], | ||
rules: { | ||
'max-len': 'off', | ||
}, | ||
}, | ||
], | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
semi: true, | ||
trailingComma: 'all', | ||
singleQuote: true, | ||
printWidth: 120, | ||
tabWidth: 2, | ||
useTabs: false, | ||
endOfLine: 'auto', | ||
spaceBeforeFunctionParen: false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,5 @@ export enum ImageFormat { | |
BMP = 'bmp', | ||
GIF = 'gif', | ||
TIFF = 'tiff', | ||
WEBP = 'webp', | ||
ICO = 'ico', | ||
JPEG = 'jpeg', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum Media { | ||
VIDEO = 'video', | ||
IMAGE = 'image', | ||
AUDIO = 'audio', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Media } from '@/types/media'; | ||
import { Media as MediaType } from '@/enum/media'; | ||
import { VideoFormat } from '@/enum/video-format'; | ||
import { AudioFormat } from '@/enum/audio-format'; | ||
import { ImageFormat } from '@/enum/image-format'; | ||
import { FfmpegAdapter } from './ffmpeg'; | ||
import { JimpAdapter } from './jimp'; | ||
|
||
export class ConversionHandler { | ||
protected conversions: Map<string, FfmpegAdapter | JimpAdapter> = new Map(); | ||
|
||
/** | ||
* Adds a new conversion to the handler. | ||
*/ | ||
handle( | ||
id: string, | ||
filePath: string, | ||
outputFormat: VideoFormat | AudioFormat | ImageFormat, | ||
saveDirectory: string, | ||
type: Media, | ||
event: Electron.IpcMainInvokeEvent, | ||
): Promise<string> { | ||
const adapter = this.getAdapter(type); | ||
|
||
return new Promise((resolve, reject) => { | ||
this.conversions.set(id, adapter); | ||
|
||
adapter | ||
.convert(id, filePath, outputFormat, saveDirectory, event) | ||
.then(() => { | ||
this.conversions.delete(id); | ||
resolve(filePath); | ||
}) | ||
.catch((error) => { | ||
this.conversions.delete(id); | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Cancels the conversion with the given ID. | ||
*/ | ||
cancel(id: string): boolean { | ||
const adapter = this.conversions.get(id); | ||
return adapter ? adapter.cancel(id) : false; | ||
} | ||
|
||
/** | ||
* Cancels all conversions. | ||
*/ | ||
cancelAll(): void { | ||
this.conversions.forEach((adapter, id) => adapter.cancel(id)); | ||
this.conversions.clear(); | ||
} | ||
|
||
/** | ||
* Returns the appropriate adapter for the given type. | ||
*/ | ||
protected getAdapter(type: Media) { | ||
switch (type) { | ||
case MediaType.IMAGE: | ||
return new JimpAdapter(); | ||
case MediaType.VIDEO: | ||
return new FfmpegAdapter(); | ||
case MediaType.AUDIO: | ||
return new FfmpegAdapter(); | ||
default: | ||
throw new Error(`Unsupported type: ${type}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.