-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from putdotio/feat/enhance-file-utils
feat: add new file utils
- Loading branch information
Showing
13 changed files
with
539 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.