This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a36f389
commit 236f79e
Showing
8 changed files
with
192 additions
and
63 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
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,9 @@ | ||
import Fault from "./Fault"; | ||
|
||
class AddFilesFault extends Fault { | ||
constructor() { | ||
super(500, "An error occured when adding your file(s)."); | ||
} | ||
} | ||
|
||
export default AddFilesFault; |
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,9 @@ | ||
import Fault from "./Fault"; | ||
|
||
class MergerDisposedFault extends Fault { | ||
constructor() { | ||
super(404, "The requested merge has been disposed."); | ||
} | ||
} | ||
|
||
export default MergerDisposedFault; |
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 |
---|---|---|
@@ -1,27 +1,87 @@ | ||
import fs from "fs-extra"; | ||
import MergerDisposedFault from "../../../errors/MergerDisposedFault"; | ||
import MergerMaxFileCountFault from "../../../errors/MergerMaxFileCountFault"; | ||
import MergerMaxFileSizeFault from "../../../errors/MergerMaxFileSizeFault"; | ||
import ConfigService from "../../ConfigService/ConfigService"; | ||
import Model from "./Model"; | ||
|
||
export interface MergerFile { | ||
path: string; | ||
size: number; | ||
} | ||
|
||
const config = ConfigService.getConfig(); | ||
|
||
class MergerModel extends Model { | ||
private files: string[] = []; | ||
private output: string | null = null; | ||
private files: MergerFile[] = []; | ||
private output: MergerFile | null = null; | ||
private disposed = false; | ||
|
||
public addFiles(...files: string[]) { | ||
public async addFiles(...files: MergerFile[]) { | ||
this.ensureNotDisposed(); | ||
if (this.output) { | ||
this.output = null; | ||
await this.setOutput(null); | ||
} | ||
|
||
for (let i = 0; i < files.length; ++i) { | ||
const file = files[i]!; | ||
|
||
if (this.getRemainingSize() < file.size) { | ||
throw new MergerMaxFileSizeFault(); | ||
} | ||
if (this.getRemainingFilesCount() < 1) { | ||
throw new MergerMaxFileCountFault(); | ||
} | ||
|
||
this.files.push(file); | ||
} | ||
this.files.push(...files); | ||
} | ||
|
||
public getFiles(): string[] { | ||
public getFiles(): MergerFile[] { | ||
return this.files; | ||
} | ||
|
||
public setOutput(output: string) { | ||
public getFilesCount(): number { | ||
return this.files.length; | ||
} | ||
|
||
public getRemainingFilesCount(): number { | ||
return config.maxMergerFileCount - this.getFilesCount(); | ||
} | ||
|
||
public getSize(): number { | ||
return this.files.reduce((acc, file) => acc + file.size, 0); | ||
} | ||
|
||
public getRemainingSize(): number { | ||
return config.maxMergerFileSize - this.getSize(); | ||
} | ||
|
||
public async setOutput(output: MergerFile | null) { | ||
this.ensureNotDisposed(); | ||
if (this.output) { | ||
await fs.remove(this.output.path); | ||
} | ||
this.output = output; | ||
} | ||
|
||
public getOutput(): string | null { | ||
public getOutput(): MergerFile | null { | ||
return this.output; | ||
} | ||
|
||
public async dispose() { | ||
this.disposed = true; | ||
const files = [...this.files, ...(this.output ? [this.output] : [])]; | ||
await Promise.all(files.map((f) => fs.remove(f.path))); | ||
this.files = []; | ||
this.output = null; | ||
} | ||
|
||
private ensureNotDisposed() { | ||
if (this.disposed) { | ||
throw new MergerDisposedFault(); | ||
} | ||
} | ||
} | ||
|
||
export default MergerModel; |
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