Skip to content

Commit

Permalink
Replaces NativeImage by Sharp
Browse files Browse the repository at this point in the history
  • Loading branch information
digimezzo committed Mar 1, 2024
1 parent 72ac222 commit 5113bad
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 325 deletions.
2 changes: 1 addition & 1 deletion angular.webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
fs: 'commonjs fs',
assert: 'commonjs assert',
crypto: 'commonjs crypto',
fs: 'commonjs fs',
http: 'commonjs http',
https: 'commonjs https',
os: 'commonjs os',
Expand All @@ -21,5 +20,6 @@ module.exports = {
net: 'commonjs net',
querystring: 'commonjs querystring',
url: 'commonjs url',
sharp: 'commonjs sharp',
},
};
4 changes: 2 additions & 2 deletions main/common/image-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class ImageProcessor {
}

async resizeAndWriteImageAsync(imageBuffer, imagePath, maxWidth, maxHeight, jpegQuality) {
sharp(imageBuffer)
await sharp(imageBuffer)
.resize(maxWidth, maxHeight)
.jpeg({
quality: jpegQuality,
})
.toFile(imagePath, (err, info) => {});
.toFile(imagePath);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ import { SubfolderNamePipe } from './ui/pipes/subfolder-name.pipe';
import { SubfoldersFilterPipe } from './ui/pipes/subfolders-filter.pipe';
import { TracksFilterPipe } from './ui/pipes/tracks-filter.pipe';
import { ZeroToBlankPipe } from './ui/pipes/zero-to-blank.pipe';
import { AlbumArtworkCacheIdFactory } from './services/album-artwork-cache/album-artwork-cache-id-factory';
import { AlbumArtworkCacheService } from './services/album-artwork-cache/album-artwork-cache.service';
import { AlbumService } from './services/album/album-service';
import { AppearanceService } from './services/appearance/appearance.service';
import { DefaultThemesCreator } from './services/appearance/default-themes-creator';
Expand Down Expand Up @@ -252,7 +250,6 @@ import { UpdateServiceBase } from './services/update/update.service.base';
import { TranslatorServiceBase } from './services/translator/translator.service.base';
import { IndexingServiceBase } from './services/indexing/indexing.service.base';
import { NavigationServiceBase } from './services/navigation/navigation.service.base';
import { AlbumArtworkCacheServiceBase } from './services/album-artwork-cache/album-artwork-cache.service.base';
import { ApplicationServiceBase } from './services/application/application.service.base';
import { AZLyricsApi } from './common/api/lyrics/a-z-lyrics.api';
import { ChartLyricsApi } from './common/api/lyrics/chart-lyrics.api';
Expand Down Expand Up @@ -503,7 +500,6 @@ export function appInitializerFactory(translate: TranslateService, injector: Inj
FileMetadataFactory,
AlbumKeyGenerator,
MimeTypes,
AlbumArtworkCacheIdFactory,
ImageProcessor,
CachedAlbumArtworkGetter,
LastfmApi,
Expand Down Expand Up @@ -581,7 +577,6 @@ export function appInitializerFactory(translate: TranslateService, injector: Inj
{ provide: TrackRepositoryBase, useClass: TrackRepository },
{ provide: FolderRepositoryBase, useClass: FolderRepository },
{ provide: ApplicationServiceBase, useClass: ApplicationService },
{ provide: AlbumArtworkCacheServiceBase, useClass: AlbumArtworkCacheService },
{ provide: NavigationServiceBase, useClass: NavigationService },
{ provide: IndexingServiceBase, useClass: IndexingService },
{ provide: TranslatorServiceBase, useClass: TranslatorService },
Expand Down
31 changes: 10 additions & 21 deletions src/app/common/image-processor.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
import { Injectable } from '@angular/core';
import { nativeImage, NativeImage, Size } from 'electron';
import * as fs from 'fs-extra';
import fetch from 'node-fetch';
import { FileAccessBase } from './io/file-access.base';
import sharp from 'sharp';

@Injectable()
export class ImageProcessor {
public constructor(private fileAccess: FileAccessBase) {}

public async convertImageBufferToFileAsync(imageBuffer: Buffer, imagePath: string): Promise<void> {
await fs.writeFile(imagePath, imageBuffer);
}

public async convertLocalImageToBufferAsync(imagePath: string): Promise<Buffer> {
const imageBuffer: Buffer = await this.fileAccess.getFileContentAsBufferAsync(imagePath);

return imageBuffer;
return await this.fileAccess.getFileContentAsBufferAsync(imagePath);
}

public async convertOnlineImageToBufferAsync(imageUrl: string): Promise<Buffer> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const response: Response = (await fetch(imageUrl)) as Response;
const imageArrayBuffer: ArrayBuffer = await response.arrayBuffer();
const imageBuffer: Buffer = Buffer.from(imageArrayBuffer);

return imageBuffer;
return Buffer.from(imageArrayBuffer);
}

public convertBufferToImageUrl(imageBuffer: Buffer): string {
return 'data:image/png;base64,' + imageBuffer.toString('base64');
}

public resizeImage(imageBuffer: Buffer, maxWidth: number, maxHeight: number, jpegQuality: number): Buffer {
let image: NativeImage = nativeImage.createFromBuffer(imageBuffer);
const imageSize: Size = image.getSize();

if (imageSize.width > maxWidth || imageSize.height > maxHeight) {
image = image.resize({ width: maxWidth, height: maxHeight, quality: 'best' });
}

return image.toJPEG(jpegQuality);
public async resizeImageAsync(imageBuffer: Buffer, maxWidth: number, maxHeight: number, jpegQuality: number): Promise<Buffer> {
return await sharp(imageBuffer)
.resize(maxWidth, maxHeight)
.jpeg({
quality: jpegQuality,
})
.toBuffer();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5113bad

Please sign in to comment.