72 lines
1.9 KiB
JavaScript
Executable file
72 lines
1.9 KiB
JavaScript
Executable file
import React from 'react';
|
|
import PureRenderMixin from 'react-addons-pure-render-mixin';
|
|
import Image from './common/Image';
|
|
import * as FileUtils from '../utils/FileUtils';
|
|
import humanize from 'humanize';
|
|
|
|
const IMAGE_RE = /\.(png|jpe?g|webp|gif)$/i;
|
|
const MAX_IMAGE_FILE_SIZE = 1024 * 1024 * 10;
|
|
|
|
const Attachment = React.createClass({
|
|
mixins: [PureRenderMixin],
|
|
|
|
propTypes: {
|
|
inlineMedia: React.PropTypes.bool,
|
|
attachment: React.PropTypes.object.isRequired,
|
|
},
|
|
|
|
getDefaultProps() {
|
|
return {
|
|
inlineMedia: true,
|
|
};
|
|
},
|
|
|
|
isImage() {
|
|
return IMAGE_RE.test(this.props.attachment.filename);
|
|
},
|
|
|
|
render() {
|
|
const attachment = this.props.attachment;
|
|
const fileType = FileUtils.classifyFileName(attachment.filename);
|
|
|
|
if (
|
|
this.props.inlineMedia &&
|
|
this.isImage() &&
|
|
attachment.width > 0 &&
|
|
attachment.height > 0 &&
|
|
attachment.size <= MAX_IMAGE_FILE_SIZE
|
|
) {
|
|
return (
|
|
<div className="attachment-image">
|
|
<a href={attachment.url} onClick={this.props.onClick} target="_blank" rel="noreferrer">
|
|
<Image
|
|
src={attachment['proxy_url']}
|
|
href={attachment.url}
|
|
width={attachment.width}
|
|
height={attachment.height}
|
|
resize={true}
|
|
onContextMenu={this.props.onContextMenu}
|
|
/>
|
|
</a>
|
|
</div>
|
|
);
|
|
} else {
|
|
return (
|
|
<div className="attachment">
|
|
<div className={`icon icon-file ${fileType}`} />
|
|
<div className="attachment-inner">
|
|
<a href={attachment.url} target="_blank" rel="noreferrer">{attachment.filename}</a>
|
|
<div className="metadata">{humanize.filesize(attachment.size)}</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
},
|
|
});
|
|
|
|
export default Attachment;
|
|
|
|
|
|
|
|
// WEBPACK FOOTER //
|
|
// ./discord_app/components/Attachment.js
|