Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new file utils #1

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading