lbry-desktop/ui/component/fileDetails/view.jsx

128 lines
4.2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as PAGES from 'constants/pages';
import * as CS from 'constants/claim_search';
2018-11-07 23:44:38 +01:00
import React, { Fragment, PureComponent } from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2019-03-05 05:46:57 +01:00
import path from 'path';
import Card from 'component/common/card';
2017-09-17 22:33:52 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
2019-11-22 22:13:00 +01:00
uri: string,
2019-04-24 16:02:08 +02:00
claim: StreamClaim,
fileInfo: FileListItem,
metadata: StreamMetadata,
2018-03-26 23:32:43 +02:00
openFolder: string => void,
contentType: string,
2018-11-07 23:44:38 +01:00
user: ?any,
2018-03-26 23:32:43 +02:00
};
2018-11-07 23:44:38 +01:00
class FileDetails extends PureComponent<Props> {
render() {
2020-04-02 18:30:26 +02:00
const { uri, claim, contentType, fileInfo, metadata, openFolder } = this.props;
2018-11-07 23:44:38 +01:00
if (!claim || !metadata) {
2019-07-21 23:31:22 +02:00
return <span className="empty">{__('Empty claim or metadata info.')}</span>;
2018-11-07 23:44:38 +01:00
}
const { languages, license } = metadata;
2017-09-17 22:33:52 +02:00
2018-11-07 23:44:38 +01:00
const mediaType = contentType || 'unknown';
const fileSize =
metadata.source && metadata.source.size
? formatBytes(metadata.source.size)
: fileInfo && fileInfo.download_path && formatBytes(fileInfo.written_bytes);
2019-05-07 23:38:29 +02:00
let downloadPath = fileInfo && fileInfo.download_path ? path.normalize(fileInfo.download_path) : null;
let downloadNote;
2019-10-13 19:41:51 +02:00
// If the path is blank, file is not available. Streamed files won't have any blobs saved
2019-08-02 08:28:14 +02:00
// Create path from name so the folder opens on click.
if (fileInfo && fileInfo.blobs_completed >= 1 && fileInfo.download_path === null) {
downloadPath = `${fileInfo.download_directory}/${fileInfo.file_name}`;
downloadNote = __('This file may have been streamed, moved or deleted');
}
2018-11-07 23:44:38 +01:00
return (
<Fragment>
<Card
title={__('File Details')}
defaultExpand={false}
actions={
<table className="table table--condensed table--fixed table--file-details">
<tbody>
<tr>
<td> {__('Content Type')}</td>
<td>{mediaType}</td>
</tr>
2020-04-02 18:30:26 +02:00
{claim && claim.meta.reposted > 0 && (
<tr>
<td>{__('Reposts')}</td>
<td>
<Button
button="link"
label={__('View %count% reposts', { count: claim.meta.reposted })}
navigate={`/$/${PAGES.DISCOVER}?${CS.REPOSTED_URI_KEY}=${encodeURIComponent(uri)}`}
/>
</td>
</tr>
)}
{fileSize && (
<tr>
<td> {__('File Size')}</td>
<td>{fileSize}</td>
</tr>
)}
<tr>
<td> {__('Claim ID')}</td>
<td>{claim.claim_id}</td>
</tr>
{languages && (
<tr>
<td>{__('Languages')}</td>
<td>{languages.join(' ')}</td>
</tr>
)}
<tr>
<td>{__('License')}</td>
<td>{license}</td>
</tr>
{downloadPath && (
<tr>
<td>{__('Downloaded to')}</td>
<td>
{/* {downloadPath.replace(/(.{10})/g, '$1\u200b')} */}
<Button
button="link"
className="button--download-link"
onClick={() => {
if (downloadPath) {
openFolder(downloadPath);
}
}}
label={downloadNote || downloadPath.replace(/(.{10})/g, '$1\u200b')}
/>
</td>
</tr>
)}
</tbody>
</table>
}
/>
2018-11-07 23:44:38 +01:00
</Fragment>
);
}
}
// move this with other helper functions when we re-use it
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return __('0 Bytes');
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [__('Bytes'), __('KB'), __('MB'), __('GB'), __('TB'), __('PB'), __('EB'), __('ZB'), __('YB')];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
2017-09-17 22:33:52 +02:00
export default FileDetails;