Added utility function to get a file attachment's url

Этот коммит содержится в:
hmhealey
2015-07-28 11:30:22 -04:00
родитель 0e121a68de
Коммит f66cd5e977
2 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@@ -71,16 +71,8 @@ module.exports = React.createClass({
var filename = filenames[this.props.index];
var fileInfo = utils.splitFileLocation(filename);
if (Object.keys(fileInfo).length === 0) return null;
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;
var thumbnail;
if (type === "image") {
thumbnail = <div ref={filename} className="post__load" style={{backgroundImage: 'url(/static/images/load.gif)'}}/>;
@@ -92,7 +84,7 @@ module.exports = React.createClass({
var self = this;
// asynchronously request the size of the file so that we can display it next to the thumbnail
utils.getFileSize(fileInfo.path + "." + fileInfo.ext, function(fileSize) {
utils.getFileSize(utils.getFileUrl(filename), function(fileSize) {
self.setState({fileSize: fileSize});
});
}

Просмотреть файл

@@ -894,3 +894,16 @@ module.exports.fileSizeToString = function(bytes) {
return bytes + "B";
}
};
// Converts a filename (like those attached to Post objects) to a url that can be used to retrieve attachments from the server.
module.exports.getFileUrl = function(filename) {
var url = filename;
// This is a temporary patch to fix issue with old files using absolute paths
if (url.indexOf("/api/v1/files/get") != -1) {
url = filename.split("/api/v1/files/get")[1];
}
url = module.exports.getWindowLocationOrigin() + "/api/v1/files/get" + url;
return url;
};