From 1756d89cb47deea3abb88623e6487756484c4305 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Fri, 27 Mar 2020 19:28:41 +0300 Subject: [PATCH] file component and lib updated --- pages/[...slug].tsx | 36 +++++++++++++++++++++--------------- src/lib/file.ts | 21 +++++++++++---------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index c0585948..6d8e0d54 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -9,6 +9,7 @@ import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IBreadcumbsPart, IMeta, IPage, ITag, FileTokenData, Token } from 'src/types' import { joinTokens } from 'src/lib/tokens' +import { size, ext } from 'src/lib/file' import { Html } from 'src/components/Html' function array(value: T | T[]) { @@ -37,25 +38,30 @@ const Youtube: React.SFC<{ url: string }> = props => { ) } -const File: React.SFC = props => ( -
- -
- {props['title']} +const File: React.SFC = props => { + const fileSize = size(props.file_size) + const format = ext(props.file_format) + + return ( + -) + ) +} type Props = { title: string diff --git a/src/lib/file.ts b/src/lib/file.ts index 504cc26d..07cfa2cd 100644 --- a/src/lib/file.ts +++ b/src/lib/file.ts @@ -1,16 +1,17 @@ -const getExt = (name: string) => /\.(\w+)$/.exec(name)[1] -const defaultExt = (ext: string) => ext.toUpperCase() -const exts = ['png', 'pdf', 'jpg', 'jpeg', 'gif', 'doc'] -const extd = ['ПНГ', 'ПДФ', 'ЖПГ', 'ЖПГ', 'ГИФ', 'ДОК'] const units = ['байт', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ'] -export function ext(filname: string): string { - const ext = getExt(filname) - const i = exts.indexOf(ext) +const mime = new Map([ + ['application/pdf', 'ПДФ'], + ['image/jpeg', 'ЖПГ'], + ['image/png', 'ПНГ'], +]) - return i > -1 - ? extd[i] - : defaultExt(ext) +export function ext(mimeType: string): string { + if (mime.has(mimeType)) { + return mime.get(mimeType) + } + + return mimeType } export function size(bytes: any, precision = 1): string {