Skip to content

Commit

Permalink
feat: utilize constants for file categorization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wilwong89 committed Jan 25, 2024
1 parent 3daeba4 commit 3ca0ca9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
19 changes: 10 additions & 9 deletions frontend/src/components/file/DocumentCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { filesize } from 'filesize';
import { Button, Card, useConfirm, useToast } from '@/lib/primevue';
import { documentService } from '@/services';
import { FILE_CATEGORIES } from '@/utils/constants';
import { formatDateLong } from '@/utils/formatters';
import { isFileCategory } from '@/utils/utils';
import { getFileCategory } from '@/utils/utils';
import compressed from '@/assets/images/compressed.svg';
import doc from '@/assets/images/doc.svg';
Expand Down Expand Up @@ -55,22 +56,22 @@ const confirmDelete = (documentId: string, filename: string) => {
};
const displayIcon = (mimeType = '') => {
const icon = isFileCategory(mimeType);
const icon = getFileCategory(mimeType);
switch (icon) {
case 'doc':
case FILE_CATEGORIES.DOC:
return doc;
case 'shape':
case FILE_CATEGORIES.SHAPE:
return shape;
case 'pdf':
case FILE_CATEGORIES.PDF:
return pdf;
case 'spreadsheet':
case FILE_CATEGORIES.SPREADSHEET:
return spreadsheet;
case 'image':
case FILE_CATEGORIES.IMAGE:
return image;
case 'email':
case FILE_CATEGORIES.EMAIL:
return email;
case 'compressed':
case FILE_CATEGORIES.COMPRESSED:
return compressed;
default:
return file;
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ export const QueuePriority = [0, 1, 2, 3, 4, 5];
* Default System User ID
*/
export const SYSTEM_USER = '00000000-0000-0000-0000-000000000000';

/**
* File category definitions
*/
export const FILE_CATEGORIES = {
COMPRESSED: 'compressed',
DOC: 'doc',
EMAIL: 'email',
FILE: 'file',
IMAGE: 'image',
PDF: 'pdf',
SHAPE: 'shape',
SPREADSHEET: 'spreadsheet'
};
22 changes: 11 additions & 11 deletions frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DELIMITER } from '@/utils/constants';
import { DELIMITER, FILE_CATEGORIES } from '@/utils/constants';

/**
* @function differential
Expand All @@ -12,47 +12,47 @@ export function differential(source: any, comparer: any): any {
}

/**
* @function isFileCategory
* @function getFileCategory
* Compares mimeType to a list and returns a string category for that mimetype
* @param {string} mimeType mimeType to be categorized
* @returns {string} Category of file the mimeType falls under
*/
export function isFileCategory(mimeType: string): string {
export function getFileCategory(mimeType: string): string {
// Checks for commonly used and likely used mimetypes
switch (true) {
case mimeType.includes('/vnd.shp'):
case mimeType.includes('/vnd.shx'):
case mimeType.includes('/dbase'):
case mimeType.includes('/dbf'):
return 'shape';
return FILE_CATEGORIES.SHAPE;
case mimeType.includes('/msword'):
case mimeType.includes('/vnd.ms-word'):
case mimeType.includes('/vnd.oasis.opendocument.text'):
case mimeType.includes('/vnd.openxmlformats-officedocument.wordprocessingml'):
return 'doc';
return FILE_CATEGORIES.DOC;
case mimeType.includes('/vnd.pdf'):
case mimeType.includes('/pdf'):
case mimeType.includes('/x-pdf'):
return 'pdf';
return FILE_CATEGORIES.PDF;
case mimeType.includes('/vnd.ms-excel'):
case mimeType.includes('/vnd.openxmlformats-officedocument.spreadsheetml'):
case mimeType.includes('/vnd.oasis.opendocument.spreadsheet'):
return 'spreadsheet';
return FILE_CATEGORIES.SPREADSHEET;
case mimeType.includes('image/'):
return 'image';
return FILE_CATEGORIES.IMAGE;
case mimeType.includes('/vnd.seemail'):
case mimeType.includes('/vnd.omads-email+xml'):
case mimeType.includes('message/'):
return 'email';
return FILE_CATEGORIES.EMAIL;
case mimeType.includes('/gzip'):
case mimeType.includes('/zip'):
case mimeType.includes('/x-gzip'):
case mimeType.includes('/x-zip'):
case mimeType.includes('/x-7z'):
case mimeType.includes('/x-rar'):
return 'compressed';
return FILE_CATEGORIES.COMPRESSED;
default:
return 'file';
return FILE_CATEGORIES.FILE;
}
}

Expand Down

0 comments on commit 3ca0ca9

Please sign in to comment.