Skip to content

Commit

Permalink
Merge pull request #1 from putdotio/feat/enhance-file-utils
Browse files Browse the repository at this point in the history
feat: add new file utils
  • Loading branch information
altaywtf authored Sep 18, 2024
2 parents d0dbc13 + df4c6d9 commit fe6bb09
Show file tree
Hide file tree
Showing 13 changed files with 539 additions and 11 deletions.
113 changes: 113 additions & 0 deletions src/file/file-render-type.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { getFileRenderType } from './file-render-type';
import type { IFile } from '@putdotio/api-client';

const baseFile: IFile = {
id: 1,
parent_id: 1,
name: 'test',
size: 100,
extension: 'txt',
file_type: 'TEXT',
content_type: 'text/plain',
crc32: '1234567890',
created_at: '2021-01-01T00:00:00Z',
};

describe('getFileRenderType', () => {
it('should return "folder" for directories', () => {
const file: IFile = {
...baseFile,
content_type: 'application/x-directory',
file_type: 'FOLDER',
};
expect(getFileRenderType(file)).toBe('folder');
});

it('should return "audio" for audio files', () => {
const file: IFile = {
...baseFile,
content_type: 'audio/mpeg',
file_type: 'AUDIO',
};
expect(getFileRenderType(file)).toBe('audio');
});

it('should return "video" for video files', () => {
const file: IFile = {
...baseFile,
content_type: 'video/mp4',
file_type: 'VIDEO',
};
expect(getFileRenderType(file)).toBe('video');
});

it('should return "text/markdown" for markdown files', () => {
const file: IFile = {
...baseFile,
content_type: 'text/markdown',
extension: 'md',
file_type: 'TEXT',
};
expect(getFileRenderType(file)).toBe('text/markdown');
});

it('should return "text" for text files', () => {
const file: IFile = {
...baseFile,
content_type: 'text/plain',
file_type: 'TEXT',
};
expect(getFileRenderType(file)).toBe('text');
});

it('should return "image" for image files', () => {
const file: IFile = {
...baseFile,
content_type: 'image/jpeg',
file_type: 'IMAGE',
};
expect(getFileRenderType(file)).toBe('image');
});

it('should return "pdf" for PDF files', () => {
const file: IFile = {
...baseFile,
content_type: 'application/pdf',
file_type: 'PDF',
};
expect(getFileRenderType(file)).toBe('pdf');
});

it('should return "archive" for archive files', () => {
const file: IFile = {
...baseFile,
content_type: 'application/zip',
file_type: 'ARCHIVE',
};
expect(getFileRenderType(file)).toBe('archive');
});

it('should return "epub" for EPUB files', () => {
const file: IFile = {
...baseFile,
content_type: 'application/epub+zip',
};
expect(getFileRenderType(file)).toBe('epub');
});

it('should return "other" for unknown file types', () => {
const file: IFile = {
...baseFile,
content_type: 'application/octet-stream',
};
expect(getFileRenderType(file)).toBe('other');
});

it('should return "other" if content_type is not a string', () => {
const file: IFile = {
...baseFile,
content_type: null as any,
};
expect(getFileRenderType(file)).toBe('other');
});
});
58 changes: 58 additions & 0 deletions src/file/file-render-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { IFile } from '@putdotio/api-client';

export type FileRenderType =
| 'archive'
| 'audio'
| 'epub'
| 'folder'
| 'image'
| 'other'
| 'pdf'
| 'text'
| 'text/markdown'
| 'video';

export const getFileRenderType = (file: IFile): FileRenderType => {
const { content_type, extension, file_type } = file;

if (typeof content_type !== 'string') {
return 'other';
}

if (content_type === 'application/x-directory') {
return 'folder';
}

if (content_type.startsWith('audio') || file_type === 'AUDIO') {
return 'audio';
}

if (content_type.startsWith('video')) {
return 'video';
}

if (content_type.startsWith('text')) {
if (content_type.endsWith('/markdown') || extension === 'md') {
return 'text/markdown';
}
return 'text';
}

if (content_type.startsWith('image')) {
return 'image';
}

if (content_type === 'application/pdf') {
return 'pdf';
}

if (['application/x-rar', 'application/zip'].includes(content_type)) {
return 'archive';
}

if (content_type === 'application/epub+zip') {
return 'epub';
}

return 'other';
};
4 changes: 2 additions & 2 deletions src/file/file.spec.ts → src/file/file-size.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toHumanFileSize } from './file';
import { toHumanFileSize } from './file-size';

describe('files', () => {
describe('file-size', () => {
describe('toHumanFileSize', () => {
it('should convert bytes to human readable format', () => {
expect(toHumanFileSize(1024)).toBe('1 KB');
Expand Down
File renamed without changes.
Loading

0 comments on commit fe6bb09

Please sign in to comment.