hostr/lib/format.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import moment from 'moment';
2018-06-02 18:07:00 +00:00
import sniff from './sniff';
2015-07-09 23:01:43 +01:00
2015-08-30 18:35:05 +02:00
const baseURL = process.env.WEB_BASE_URL;
2015-07-09 23:01:43 +01:00
export function formatDate(timestamp) {
return moment.unix(timestamp).format('D MMM YY [at] h:mm A');
}
export function formatSize(size) {
if (size >= 1073741824) {
2016-06-06 15:37:00 +01:00
return `${Math.round((size / 1073741824) * 10) / 10}GB`;
2015-07-09 23:01:43 +01:00
}
2015-08-23 22:12:32 +01:00
if (size >= 1048576) {
2016-06-06 15:37:00 +01:00
return `${Math.round((size / 1048576) * 10) / 10}MB`;
2015-08-23 22:12:32 +01:00
}
if (size >= 1024) {
2016-06-06 15:37:00 +01:00
return `${Math.round((size / 1024) * 10) / 10}KB`;
2015-08-23 22:12:32 +01:00
}
2016-06-06 15:37:00 +01:00
return `${Math.round(size)}B`;
2015-08-23 22:12:32 +01:00
}
2015-07-09 23:01:43 +01:00
export function formatFile(file) {
const formattedFile = {
2016-08-07 14:38:05 +01:00
added: moment.unix(file.createdAt / 1000).format(),
readableAdded: formatDate(file.createdAt / 1000),
2015-07-09 23:01:43 +01:00
downloads: file.downloads !== undefined ? file.downloads : 0,
2016-06-19 10:14:47 -07:00
href: `${baseURL}/${file.id}`,
id: file.id,
name: file.name,
size: file.size,
readableSize: formatSize(file.size),
type: sniff(file.name),
2015-07-09 23:01:43 +01:00
trashed: (file.status === 'trashed'),
2016-08-07 14:38:05 +01:00
status: file.processed === true ? 'active' : 'uploading',
2015-07-09 23:01:43 +01:00
};
if (file.width) {
formattedFile.height = file.height;
formattedFile.width = file.width;
2016-06-19 10:14:47 -07:00
const ext = (file.name.split('.').pop().toLowerCase() === 'psd' ? '.png' : '');
2015-07-09 23:01:43 +01:00
formattedFile.direct = {
2016-06-19 10:14:47 -07:00
'150x': `${baseURL}/file/150/${file.id}/${file.name}${ext}`,
'970x': `${baseURL}/file/970/${file.id}/${file.name}${ext}`,
2015-07-09 23:01:43 +01:00
};
}
return formattedFile;
}