Skip to content

Commit af83b02

Browse files
committed
change options of octopus-psd reader constructor
1 parent b8adaf9 commit af83b02

File tree

6 files changed

+35
-38
lines changed

6 files changed

+35
-38
lines changed

packages/octopus-psd/examples/node/convert-debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function convertDesign({
5959
console.log(`\n${chalk.yellow(`Statistics:`)} file://${statistics}\n\n`)
6060
})
6161

62-
const reader = await PSDFileReader.withRenderer({ path: filePath, designId })
62+
const reader = await PSDFileReader.withRenderer({ path: filePath })
6363
const sourceDesign = await reader.getSourceDesign()
6464
if (sourceDesign === null) {
6565
console.error('Creating SourceDesign Failed')

packages/octopus-psd/examples/node/convert-local.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async function convert() {
1212
const reader = await PSDFileReader.withRenderer({ path: filePath })
1313

1414
const sourceDesign = await reader.getSourceDesign()
15+
1516
if (sourceDesign === null) {
1617
console.error('Creating SourceDesign Failed')
1718
return

packages/octopus-psd/examples/web/index-api.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
<script>
1313
window.addEventListener('DOMContentLoaded', async () => {
1414
const { createConverter, PSDFileReader, WebExporter } = OctopusPSD
15-
const readerOptions = {
16-
path: './smiley.psd',
17-
}
18-
1915
try {
16+
const response = await fetch('./smiley.psd')
17+
const file =new Uint8Array(await response.arrayBuffer())
2018
const converter = createConverter()
2119

22-
const reader = await PSDFileReader.withRenderer(readerOptions)
20+
const reader = await PSDFileReader.withRenderer({file})
2321
const sourceDesign = await reader.getSourceDesign()
2422

2523
if (sourceDesign === null) {

packages/octopus-psd/src/services/readers/node/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ export type PSDFileReaderOptions = WithRendererOptions & {
1616
export type WithRendererOptions = {
1717
/** Path to the .psd design file. */
1818
path: string
19-
20-
/** Unique ID. If not passed, will be generated by UUIDv4 */
21-
designId?: string
2219
}
2320

2421
/**
2522
* Reader that converts Adobe Photoshop .psd file into `SourceDesign` object.
2623
*/
2724
export class PSDFileReader extends PSDFileReaderCommon {
25+
private _path: string
26+
2827
static async withRenderer(options: WithRendererOptions): Promise<PSDFileReader> {
2928
if (!PSDFileReaderCommon._renderer) {
3029
PSDFileReaderCommon._renderer = await getRenderer()
@@ -39,8 +38,13 @@ export class PSDFileReader extends PSDFileReaderCommon {
3938
* @param {PSDFileReaderOptions} options
4039
*/
4140
constructor(options: PSDFileReaderOptions) {
42-
const promisedData = readFile(options.path)
43-
super({ ...options, promisedData })
41+
super({ ...options })
42+
this._path = options.path
43+
}
44+
45+
protected async _getSourceData(): Promise<Uint8Array> {
46+
const buffer = await readFile(this._path)
47+
return new Uint8Array(buffer)
4448
}
4549

4650
protected async _convertImage({ width, height, buff, name, iccProfile }: ConvertImageOptions): Promise<void> {

packages/octopus-psd/src/services/readers/psd-file-reader-common.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ type ProcessedImage = {
4141

4242
export type PSDFileReaderOptions = {
4343
/** Path to the .psd design file. */
44-
path: string
44+
path?: string
4545

46-
/**File read to buffer */
47-
promisedData: Promise<Uint8Array>
48-
/** Unique ID. If not passed, will be generated by UUIDv4 */
49-
designId?: string
5046
/** wasm image processing tool */
5147
renderer?: Renderer
5248
}
@@ -55,13 +51,11 @@ export type PSDFileReaderOptions = {
5551
* Reader that converts Adobe Photoshop .psd file into `SourceDesign` object.
5652
*/
5753
export abstract class PSDFileReaderCommon {
58-
private _promisedData: Promise<Uint8Array>
59-
protected _path: string
6054
private _designId: string
6155
protected _images: ProcessedImage[] = []
6256
protected _renderer?: Renderer
6357
private _uniqueId: () => string
64-
private _promisePsd: Promise<Psd | null>
58+
private _psd: Psd
6559

6660
static _renderer: Renderer
6761
static SHAPE_LAYER_KEYS = [
@@ -101,12 +95,9 @@ export abstract class PSDFileReaderCommon {
10195
* @param {PSDFileReaderOptions} options
10296
*/
10397
constructor(options: PSDFileReaderOptions) {
104-
this._promisedData = options.promisedData
10598
this._uniqueId = uniqueIdFactory(1000000)
106-
this._path = options.path
10799
this._renderer = options.renderer
108-
this._designId = options.designId || uuidv4()
109-
this._promisePsd = this._getPsd()
100+
this._designId = uuidv4()
110101
}
111102

112103
/**
@@ -241,8 +232,14 @@ export abstract class PSDFileReaderCommon {
241232
})
242233
}
243234

235+
protected abstract _getSourceData(): Promise<Uint8Array>
236+
244237
private async _getPsd(): Promise<Psd> {
245-
const data = await this._promisedData
238+
if (this._psd) {
239+
return this._psd
240+
}
241+
242+
const data = await this._getSourceData()
246243

247244
const psd = Psd.parse(data.buffer)
248245
this._assignMissingLayerIds(psd)
@@ -279,6 +276,8 @@ export abstract class PSDFileReaderCommon {
279276

280277
psdCopy.children.push(child as unknown as Layer)
281278

279+
this._psd = psdCopy
280+
282281
return psdCopy
283282
}
284283

@@ -300,7 +299,7 @@ export abstract class PSDFileReaderCommon {
300299
* Returns `FileMeta` with list of Artboards and designName
301300
*/
302301
async readFileMeta(): Promise<FileMeta | null> {
303-
const psd = await this._promisePsd
302+
const psd = await this._getPsd()
304303
if (!psd) {
305304
return null
306305
}
@@ -326,7 +325,7 @@ export abstract class PSDFileReaderCommon {
326325
*/
327326
async getSourceDesign(componentIds?: number[]): Promise<SourceDesign | null> {
328327
const designId = this.designId
329-
const psd = await this._promisePsd
328+
const psd = await this._getPsd()
330329

331330
if (!psd) return null
332331

packages/octopus-psd/src/services/readers/web/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@ export type PSDFileReaderOptions = WithRendererOptions & {
1212

1313
export type WithRendererOptions = {
1414
/** Path to the .psd design file. */
15-
path: string
16-
17-
/** Unique ID. If not passed, will be generated by UUIDv4 */
18-
designId?: string
15+
file: Uint8Array
1916
}
2017

2118
export class PSDFileReader extends PSDFileReaderCommon {
22-
static async readFile(filePath: string): Promise<Uint8Array> {
23-
const response = await fetch(filePath)
24-
const buffer = await response.arrayBuffer()
25-
26-
return new Uint8Array(buffer)
27-
}
19+
private _file: Uint8Array
2820

2921
static async withRenderer(options: WithRendererOptions): Promise<PSDFileReaderCommon> {
3022
if (!PSDFileReaderCommon._renderer) {
@@ -40,9 +32,12 @@ export class PSDFileReader extends PSDFileReaderCommon {
4032
* @param {PSDFileReaderOptions} options
4133
*/
4234
constructor(options: PSDFileReaderOptions) {
43-
const promisedData = PSDFileReader.readFile(options.path)
35+
super({ ...options })
36+
this._file = options.file
37+
}
4438

45-
super({ ...options, promisedData })
39+
protected async _getSourceData(): Promise<Uint8Array> {
40+
return Promise.resolve(this._file)
4641
}
4742

4843
protected async _convertImage({ width, height, buff, name, iccProfile }: ConvertImageOptions): Promise<void> {

0 commit comments

Comments
 (0)