diff --git a/api/file.go b/api/file.go
index 3ef50fbbd5..219cf61037 100644
--- a/api/file.go
+++ b/api/file.go
@@ -33,7 +33,7 @@ func InitFile(r *mux.Router) {
sr := r.PathPrefix("/files").Subrouter()
sr.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST")
- sr.Handle("/get/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFile)).Methods("GET")
+ sr.Handle("/get/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFile)).Methods("GET", "HEAD")
sr.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("POST")
}
@@ -261,7 +261,10 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=2592000, public")
w.Header().Set("Content-Length", strconv.Itoa(len(f)))
- w.Write(f)
+
+ if r.Method != "HEAD" {
+ w.Write(f)
+ }
}
func asyncGetFile(path string, fileData chan []byte) {
diff --git a/web/react/components/file_attachment.jsx b/web/react/components/file_attachment.jsx
new file mode 100644
index 0000000000..3cd7918873
--- /dev/null
+++ b/web/react/components/file_attachment.jsx
@@ -0,0 +1,121 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+var utils = require('../utils/utils.jsx');
+
+module.exports = React.createClass({
+ displayName: "FileAttachment",
+ canSetState: false,
+ propTypes: {
+ // a list of file pathes displayed by the parent FileAttachmentList
+ filenames: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
+ // the index of this attachment preview in the parent FileAttachmentList
+ index: React.PropTypes.number.isRequired,
+ // the identifier of the modal dialog used to preview files
+ modalId: React.PropTypes.string.isRequired,
+ // handler for when the thumbnail is clicked
+ handleImageClick: React.PropTypes.func
+ },
+ getInitialState: function() {
+ return {fileSize: -1};
+ },
+ componentDidMount: function() {
+ this.canSetState = true;
+
+ var filename = this.props.filenames[this.props.index];
+
+ if (filename) {
+ var fileInfo = utils.splitFileLocation(filename);
+ var type = utils.getFileType(fileInfo.ext);
+
+ // This is a temporary patch to fix issue with old files using absolute paths
+ if (fileInfo.path.indexOf("/api/v1/files/get") != -1) {
+ fileInfo.path = fileInfo.path.split("/api/v1/files/get")[1];
+ }
+ fileInfo.path = utils.getWindowLocationOrigin() + "/api/v1/files/get" + fileInfo.path;
+
+ if (type === "image") {
+ var self = this;
+ $('').attr('src', fileInfo.path+'_thumb.jpg').load(function(path, name){ return function() {
+ $(this).remove();
+ if (name in self.refs) {
+ var imgDiv = self.refs[name].getDOMNode();
+
+ $(imgDiv).removeClass('post__load');
+ $(imgDiv).addClass('post__image');
+
+ var re1 = new RegExp(' ', 'g');
+ var re2 = new RegExp('\\(', 'g');
+ var re3 = new RegExp('\\)', 'g');
+ var url = path.replace(re1, '%20').replace(re2, '%28').replace(re3, '%29');
+ $(imgDiv).css('background-image', 'url('+url+'_thumb.jpg)');
+ }
+ }}(fileInfo.path, filename));
+ }
+ }
+ },
+ componentWillUnmount: function() {
+ // keep track of when this component is mounted so that we can asynchronously change state without worrying about whether or not we're mounted
+ this.canSetState = false;
+ },
+ shouldComponentUpdate: function(nextProps, nextState) {
+ // the only time this object should update is when it receives an updated file size which we can usually handle without re-rendering
+ if (nextState.fileSize != this.state.fileSize) {
+ if (this.refs.fileSize) {
+ // update the UI element to display the file size without re-rendering the whole component
+ this.refs.fileSize.getDOMNode().innerHTML = utils.fileSizeToString(nextState.fileSize);
+
+ return false;
+ } else {
+ // we can't find the element that should hold the file size so we must not have rendered yet
+ return true;
+ }
+ } else {
+ return true;
+ }
+ },
+ render: function() {
+ var filenames = this.props.filenames;
+ var filename = filenames[this.props.index];
+
+ var fileInfo = utils.splitFileLocation(filename);
+ var type = utils.getFileType(fileInfo.ext);
+
+ var thumbnail;
+ if (type === "image") {
+ thumbnail =
;
+ } else {
+ thumbnail = ;
+ }
+
+ var fileSizeString = "";
+ if (this.state.fileSize < 0) {
+ var self = this;
+
+ // asynchronously request the size of the file so that we can display it next to the thumbnail
+ utils.getFileSize(utils.getFileUrl(filename), function(fileSize) {
+ if (self.canSetState) {
+ self.setState({fileSize: fileSize});
+ }
+ });
+ } else {
+ fileSizeString = utils.fileSizeToString(this.state.fileSize);
+ }
+
+ return (
+
+ );
+ }
+});
diff --git a/web/react/components/file_attachment_list.jsx b/web/react/components/file_attachment_list.jsx
new file mode 100644
index 0000000000..b924429577
--- /dev/null
+++ b/web/react/components/file_attachment_list.jsx
@@ -0,0 +1,49 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+var ViewImageModal = require('./view_image.jsx');
+var FileAttachment = require('./file_attachment.jsx');
+var Constants = require('../utils/constants.jsx');
+
+module.exports = React.createClass({
+ displayName: "FileAttachmentList",
+ propTypes: {
+ // a list of file pathes displayed by this
+ filenames: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
+ // the identifier of the modal dialog used to preview files
+ modalId: React.PropTypes.string.isRequired,
+ // the channel that this is part of
+ channelId: React.PropTypes.string,
+ // the user that owns the post that this is attached to
+ userId: React.PropTypes.string
+ },
+ getInitialState: function() {
+ return {startImgId: 0};
+ },
+ render: function() {
+ var filenames = this.props.filenames;
+ var modalId = this.props.modalId;
+
+ var postFiles = [];
+ for (var i = 0; i < filenames.length && i < Constants.MAX_DISPLAY_FILES; i++) {
+ postFiles.push();
+ }
+
+ return (
+
+
+ {postFiles}
+
+
+
+ );
+ },
+ handleImageClick: function(e) {
+ this.setState({startImgId: parseInt($(e.target.parentNode).attr('data-img-id'))});
+ }
+});
diff --git a/web/react/components/post_body.jsx b/web/react/components/post_body.jsx
index 641ffeef27..860c96d842 100644
--- a/web/react/components/post_body.jsx
+++ b/web/react/components/post_body.jsx
@@ -1,63 +1,23 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
-var CreateComment = require( './create_comment.jsx' );
+var FileAttachmentList = require('./file_attachment_list.jsx');
var UserStore = require('../stores/user_store.jsx');
var utils = require('../utils/utils.jsx');
-var ViewImageModal = require('./view_image.jsx');
-var Constants = require('../utils/constants.jsx');
module.exports = React.createClass({
- handleImageClick: function(e) {
- this.setState({startImgId: parseInt($(e.target.parentNode).attr('data-img-id'))});
- },
componentWillReceiveProps: function(nextProps) {
var linkData = utils.extractLinks(nextProps.post.message);
this.setState({ links: linkData["links"], message: linkData["text"] });
},
- componentDidMount: function() {
- var filenames = this.props.post.filenames;
- var self = this;
- if (filenames) {
- var re1 = new RegExp(' ', 'g');
- var re2 = new RegExp('\\(', 'g');
- var re3 = new RegExp('\\)', 'g');
- for (var i = 0; i < filenames.length && i < Constants.MAX_DISPLAY_FILES; i++) {
- var fileInfo = utils.splitFileLocation(filenames[i]);
- if (Object.keys(fileInfo).length === 0) continue;
-
- var type = utils.getFileType(fileInfo.ext);
-
- // This is a temporary patch to fix issue with old files using absolute paths
- if (fileInfo.path.indexOf("/api/v1/files/get") != -1) {
- fileInfo.path = fileInfo.path.split("/api/v1/files/get")[1];
- }
- fileInfo.path = utils.getWindowLocationOrigin() + "/api/v1/files/get" + fileInfo.path;
-
- if (type === "image") {
- $('').attr('src', fileInfo.path+'_thumb.jpg').load(function(path, name){ return function() {
- $(this).remove();
- if (name in self.refs) {
- var imgDiv = self.refs[name].getDOMNode();
- $(imgDiv).removeClass('post__load');
- $(imgDiv).addClass('post__image');
- var url = path.replace(re1, '%20').replace(re2, '%28').replace(re3, '%29');
- $(imgDiv).css('background-image', 'url('+url+'_thumb.jpg)');
- }
- }}(fileInfo.path, filenames[i]));
- }
- }
- }
- },
getInitialState: function() {
var linkData = utils.extractLinks(this.props.post.message);
- return { startImgId: 0, links: linkData["links"], message: linkData["text"] };
+ return { links: linkData["links"], message: linkData["text"] };
},
render: function() {
var post = this.props.post;
var filenames = this.props.post.filenames;
var parentPost = this.props.parentPost;
- var postImageModalId = "view_image_modal_" + post.id;
var inner = utils.textToJsx(this.state.message);
var comment = "";
@@ -99,44 +59,8 @@ module.exports = React.createClass({
postClass += " post-comment";
}
- var postFiles = [];
- var images = [];
- if (filenames) {
- for (var i = 0; i < filenames.length; i++) {
- var fileInfo = utils.splitFileLocation(filenames[i]);
- if (Object.keys(fileInfo).length === 0) continue;
-
- var type = utils.getFileType(fileInfo.ext);
-
- // This is a temporary patch to fix issue with old files using absolute paths
- if (fileInfo.path.indexOf("/api/v1/files/get") != -1) {
- fileInfo.path = fileInfo.path.split("/api/v1/files/get")[1];
- }
- fileInfo.path = utils.getWindowLocationOrigin() + "/api/v1/files/get" + fileInfo.path;
-
- if (type === "image") {
- if (i < Constants.MAX_DISPLAY_FILES) {
- postFiles.push(
-
-
-
- );
- }
- images.push(filenames[i]);
- } else if (i < Constants.MAX_DISPLAY_FILES) {
- postFiles.push(
-
+ );
+
+ // asynchronously request the actual size of this file
+ if (!(filename in this.state.fileSizes)) {
+ var self = this;
+
+ utils.getFileSize(utils.getFileUrl(filename), function(fileSize) {
+ if (self.canSetState) {
+ var fileSizes = self.state.fileSizes;
+ fileSizes[filename] = fileSize;
+ self.setState(fileSizes);
+ }
+ });
+ }
+ }
+ } else {
+ // display a progress indicator when the preview for an image is still loading
var percentage = Math.floor(this.state.progress[this.state.imgId]);
- loading = (
-