-
Notifications
You must be signed in to change notification settings - Fork 598
/
upload_viewer.jsx
160 lines (146 loc) · 5.9 KB
/
upload_viewer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { setUploadViewerMetadata, setUploadPageViews, resetUploadsViews } from '../../actions/uploads_actions.js';
import { formatDateWithoutTime } from '../../utils/date_utils.js';
import { forEach, get } from 'lodash-es';
import OnClickOutside from 'react-onclickoutside';
const UploadViewer = ({ closeUploadViewer, upload, imageFile }) => {
const dispatch = useDispatch();
const uploadMetadata = useSelector(state => state.uploads.uploadMetadata);
const pageViews = useSelector(state => state.uploads.averageViews);
const [loadingViews, setLoadingViews] = useState(true);
useEffect(() => {
dispatch(setUploadViewerMetadata(upload));
return () => {
dispatch(resetUploadsViews());
};
}, [upload]);
useEffect(() => {
const metadata = get(uploadMetadata, `query.pages[${upload.id}]`);
const fileUsage = get(metadata, 'globalusage', []);
if (fileUsage && loadingViews) {
handleGetFileViews(fileUsage);
}
}, [uploadMetadata, upload.id, loadingViews]);
const handleGetFileViews = (files) => {
dispatch(setUploadPageViews(files));
setLoadingViews(false);
};
const handleClickOutside = () => {
closeUploadViewer();
};
const metadata = get(uploadMetadata, `query.pages[${upload.id}]`);
const imageDescription = get(metadata, 'imageinfo[0].extmetadata.ImageDescription.value');
const width = get(metadata, 'imageinfo[0].width');
const height = get(metadata, 'imageinfo[0].height');
let size = get(metadata, 'imageinfo[0].size');
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(size) / Math.log(1024));
size = `${parseFloat((size / (1024 ** i)).toFixed(2))} ${sizes[i]}`;
const imageUrl = get(metadata, 'imageinfo[0].url');
const profileLink = `/users/${encodeURIComponent(upload.uploader)}`;
const author = <a href={profileLink} target="_blank">{upload.uploader}</a>;
const source = get(metadata, 'imageinfo[0].extmetadata.Credit.value');
const license = get(metadata, 'imageinfo[0].extmetadata.LicenseShortName.value');
const globalUsage = get(metadata, 'globalusage', []);
let usageTableElements;
if (globalUsage && pageViews !== undefined) {
usageTableElements = globalUsage.map((usage, index) => {
return (
<tr className="view-file-details" key={usage.url}>
<td className="row-details">{usage.wiki} </td>
<td className="row-details"><a href={usage.url}>{usage.title}</a> </td>
<td className="text-right row-details">{pageViews[index]}</td>
</tr>
);
});
}
let fileUsageTable;
if (globalUsage.length > 0) {
fileUsageTable = (
<div>
<h1>{'\n'}</h1>
<h4>{I18n.t('uploads.file_usage')}</h4>
<table border="1">
<thead>
<tr>
<th>{I18n.t('uploads.wiki_big')}</th>
<th>{I18n.t('uploads.article_name')}</th>
<th>{I18n.t('uploads.views_per_day')}</th>
</tr>
</thead>
<tbody>
{usageTableElements}
</tbody>
</table>
</div>
);
}
let categoriesList = [];
let categories;
forEach(get(metadata, 'categories', []), (category) => {
categoriesList.push(<span key={`span-${category.title}`}> | </span>);
categoriesList.push(<a href={`https://commons.wikimedia.org/wiki/${category.title}`} target="_blank" key={`link-${category.title}`}>{category.title.slice('Category:'.length)}</a>);
});
if (categoriesList.length > 0) {
categoriesList = categoriesList.splice(1);
categories = (
<div>
<h1>{'\n'}</h1>
<h4>{I18n.t('uploads.categories')}</h4>
{categoriesList}
</div>
);
}
return (
<div className="module upload-viewer">
<div className="modal-header">
<button className="pull-right icon-close" onClick={handleClickOutside} />
<h3>{upload.file_name}</h3>
</div>
<div className="modal-body">
<div className="left">
<a href={upload.url} target="_blank"><img alt={upload.file_name} src={imageFile} /></a>
<p><a href={imageUrl} target="_blank">{I18n.t('uploads.original_file')}</a>{` (${width} X ${height} pixels, file size: ${size})`}</p>
<h4>{I18n.t('uploads.description')}</h4>
<p dangerouslySetInnerHTML={{ __html: imageDescription }} />
</div>
<div className="right">
<table className="view-file-details">
<tbody>
<tr>
<td className="row-details bg-grey">{I18n.t('uploads.date')} </td>
<td className="row-details">{formatDateWithoutTime(upload.uploaded_at)}</td>
</tr>
<tr>
<td className="row-details bg-grey">{I18n.t('uploads.author')} </td>
<td className="row-details">{author}</td>
</tr>
<tr>
<td className="row-details bg-grey">{I18n.t('uploads.source')} </td>
<td className="row-details" dangerouslySetInnerHTML={{ __html: source }} />
</tr>
<tr>
<td className="row-details bg-grey">{I18n.t('uploads.license')} </td>
<td className="row-details">{license}</td>
<td>{'\n'}</td>
</tr>
</tbody>
</table>
{categories}
{fileUsageTable}
</div>
</div>
<div className="modal-footer">
<a className="button dark small pull-right upload-viewer-button" href={upload.url} target="_blank">{I18n.t('uploads.view_commons')}</a>
</div>
</div>
);
};
UploadViewer.propTypes = {
upload: PropTypes.object,
closeUploadViewer: PropTypes.func,
imageFile: PropTypes.string
};
export default OnClickOutside(UploadViewer);