feat: add utility service for handling file types
This commit is contained in:
52
src/app/services/filetype-utils.spec.ts
Normal file
52
src/app/services/filetype-utils.spec.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { FiletypeUtils } from './filetype-utils';
|
||||||
|
|
||||||
|
describe('Filetype', () => {
|
||||||
|
let service: FiletypeUtils;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(FiletypeUtils);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should validate image mimetypes', () => {
|
||||||
|
expect(service.isValidImageMimeType('image/png')).toBe(true);
|
||||||
|
expect(service.isValidImageMimeType('text/lua')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getImageMimeTypeFromFilename', () => {
|
||||||
|
it('should return appropriate MimeType for valid file extension', () => {
|
||||||
|
const FILENAME_MOCK = 'mock.png';
|
||||||
|
const mimeType = service.getImageMimeTypeFromFilename(FILENAME_MOCK);
|
||||||
|
expect(mimeType).toEqual('image/png');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error for files without extension', async () => {
|
||||||
|
const FILENAME_MOCK = 'mock';
|
||||||
|
expect(() => service.getImageMimeTypeFromFilename(FILENAME_MOCK)).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error for unknown file extensions', () => {
|
||||||
|
const FILENAME_MOCK = 'mock.txt';
|
||||||
|
expect(() => service.getImageMimeTypeFromFilename(FILENAME_MOCK)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getImageExtensionFromMimeType', () => {
|
||||||
|
it('should return appropriate file extension for valid MimeType', () => {
|
||||||
|
const MIMETYPE_MOCK = 'image/jpeg';
|
||||||
|
const fileExt = service.getImageExtensionFromMimeType(MIMETYPE_MOCK);
|
||||||
|
expect(fileExt).toEqual('jpeg');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error for uknown mimetypes', () => {
|
||||||
|
const MIMETYPE_MOCK = 'text/lua';
|
||||||
|
expect(() => service.getImageExtensionFromMimeType(MIMETYPE_MOCK)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
44
src/app/services/filetype-utils.ts
Normal file
44
src/app/services/filetype-utils.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class FiletypeUtils {
|
||||||
|
|
||||||
|
isValidImageMimeType(mimeType: string) {
|
||||||
|
try {
|
||||||
|
this.getImageExtensionFromMimeType(mimeType);
|
||||||
|
return true;
|
||||||
|
}catch(e) {
|
||||||
|
console.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getImageMimeTypeFromFilename(filename: string): string {
|
||||||
|
const ext = filename.split('.')[1]?.toLowerCase();
|
||||||
|
if (!ext) throw new Error('File has no extension!');
|
||||||
|
const mimeMap: Record<string, string> = {
|
||||||
|
jpg: 'image/jpeg',
|
||||||
|
jpeg: 'image/jpeg',
|
||||||
|
png: 'image/png',
|
||||||
|
webp: 'image/webp',
|
||||||
|
gif: 'image/gif',
|
||||||
|
};
|
||||||
|
if (!mimeMap[ext]) throw new Error('Invalid file extension!');
|
||||||
|
return mimeMap[ext];
|
||||||
|
}
|
||||||
|
|
||||||
|
getImageExtensionFromMimeType(mimeType: string) {
|
||||||
|
const extMap: Record<string, string> = {
|
||||||
|
'image/jpeg': 'jpeg',
|
||||||
|
'image/png': 'png',
|
||||||
|
'image/webp': 'webp',
|
||||||
|
'image/gif': 'gif',
|
||||||
|
};
|
||||||
|
if (!extMap[mimeType]) throw new Error('Invalid mimetype!');
|
||||||
|
return extMap[mimeType];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user