Fixes mm-1912 move get file info into its own web service call
Этот коммит содержится в:
76
api/file.go
76
api/file.go
@@ -30,12 +30,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fileInfoCache *utils.Cache = utils.NewLru(1000)
|
||||||
|
|
||||||
func InitFile(r *mux.Router) {
|
func InitFile(r *mux.Router) {
|
||||||
l4g.Debug("Initializing file api routes")
|
l4g.Debug("Initializing file api routes")
|
||||||
|
|
||||||
sr := r.PathPrefix("/files").Subrouter()
|
sr := r.PathPrefix("/files").Subrouter()
|
||||||
sr.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST")
|
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", "HEAD")
|
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_info/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandler(getFileInfo)).Methods("GET")
|
||||||
sr.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("POST")
|
sr.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,9 +216,72 @@ type ImageGetResult struct {
|
|||||||
ImageData []byte
|
ImageData []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFileInfo(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
|
c.Err = model.NewAppError("getFileInfo", "Unable to get file info. Amazon S3 not configured and local server storage turned off. ", "")
|
||||||
|
c.Err.StatusCode = http.StatusNotImplemented
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params := mux.Vars(r)
|
||||||
|
|
||||||
|
channelId := params["channel_id"]
|
||||||
|
if len(channelId) != 26 {
|
||||||
|
c.SetInvalidParam("getFileInfo", "channel_id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userId := params["user_id"]
|
||||||
|
if len(userId) != 26 {
|
||||||
|
c.SetInvalidParam("getFileInfo", "user_id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := params["filename"]
|
||||||
|
if len(filename) == 0 {
|
||||||
|
c.SetInvalidParam("getFileInfo", "filename")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId)
|
||||||
|
|
||||||
|
path := "teams/" + c.Session.TeamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
||||||
|
size := ""
|
||||||
|
|
||||||
|
if s, ok := fileInfoCache.Get(path); ok {
|
||||||
|
size = s.(string)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
fileData := make(chan []byte)
|
||||||
|
asyncGetFile(path, fileData)
|
||||||
|
|
||||||
|
f := <-fileData
|
||||||
|
|
||||||
|
if f == nil {
|
||||||
|
c.Err = model.NewAppError("getFileInfo", "Could not find file.", "path="+path)
|
||||||
|
c.Err.StatusCode = http.StatusNotFound
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
size = strconv.Itoa(len(f))
|
||||||
|
fileInfoCache.Add(path, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.HasPermissionsToChannel(cchan, "getFileInfo") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Cache-Control", "max-age=2592000, public")
|
||||||
|
|
||||||
|
result := make(map[string]string)
|
||||||
|
result["filename"] = filename
|
||||||
|
result["size"] = size
|
||||||
|
w.Write([]byte(model.MapToJson(result)))
|
||||||
|
}
|
||||||
|
|
||||||
func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
if !utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
if !utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
c.Err = model.NewAppError("getFile", "Unable to upload file. Amazon S3 not configured and local server storage turned off. ", "")
|
c.Err = model.NewAppError("getFile", "Unable to get file. Amazon S3 not configured and local server storage turned off. ", "")
|
||||||
c.Err.StatusCode = http.StatusNotImplemented
|
c.Err.StatusCode = http.StatusNotImplemented
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -281,11 +347,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Cache-Control", "max-age=2592000, public")
|
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) {
|
func asyncGetFile(path string, fileData chan []byte) {
|
||||||
|
|||||||
@@ -201,6 +201,15 @@ func TestGetFile(t *testing.T) {
|
|||||||
t.Fatal(downErr)
|
t.Fatal(downErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp, downErr := Client.GetFileInfo(filenames[0]); downErr != nil {
|
||||||
|
t.Fatal(downErr)
|
||||||
|
} else {
|
||||||
|
m := resp.Data.(map[string]string)
|
||||||
|
if len(m["size"]) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||||
team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team)
|
team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team)
|
||||||
|
|
||||||
|
|||||||
@@ -589,6 +589,24 @@ func (c *Client) GetFile(url string, isFullUrl bool) (*Result, *AppError) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetFileInfo(url string) (*Result, *AppError) {
|
||||||
|
var rq *http.Request
|
||||||
|
rq, _ = http.NewRequest("GET", c.Url+"/files/get_info"+url, nil)
|
||||||
|
|
||||||
|
if len(c.AuthToken) > 0 {
|
||||||
|
rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rp, err := c.HttpClient.Do(rq); err != nil {
|
||||||
|
return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error())
|
||||||
|
} else if rp.StatusCode >= 300 {
|
||||||
|
return nil, AppErrorFromJson(rp.Body)
|
||||||
|
} else {
|
||||||
|
return &Result{rp.Header.Get(HEADER_REQUEST_ID),
|
||||||
|
rp.Header.Get(HEADER_ETAG_SERVER), MapFromJson(rp.Body)}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) GetPublicLink(data map[string]string) (*Result, *AppError) {
|
func (c *Client) GetPublicLink(data map[string]string) (*Result, *AppError) {
|
||||||
if r, err := c.DoPost("/files/get_public_link", MapToJson(data)); err != nil {
|
if r, err := c.DoPost("/files/get_public_link", MapToJson(data)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// See License.txt for license information.
|
// See License.txt for license information.
|
||||||
|
|
||||||
var utils = require('../utils/utils.jsx');
|
var utils = require('../utils/utils.jsx');
|
||||||
|
var Client = require('../utils/client.jsx');
|
||||||
var Constants = require('../utils/constants.jsx');
|
var Constants = require('../utils/constants.jsx');
|
||||||
|
|
||||||
module.exports = React.createClass({
|
module.exports = React.createClass({
|
||||||
@@ -103,12 +104,16 @@ module.exports = React.createClass({
|
|||||||
if (this.state.fileSize < 0) {
|
if (this.state.fileSize < 0) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// asynchronously request the size of the file so that we can display it next to the thumbnail
|
Client.getFileInfo(
|
||||||
utils.getFileSize(utils.getFileUrl(filename), function(fileSize) {
|
filename,
|
||||||
if (self.canSetState) {
|
function(data) {
|
||||||
self.setState({fileSize: fileSize});
|
if (self.canSetState) {
|
||||||
|
self.setState({fileSize: parseInt(data["size"], 10)});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
} else {
|
} else {
|
||||||
fileSizeString = utils.fileSizeToString(this.state.fileSize);
|
fileSizeString = utils.fileSizeToString(this.state.fileSize);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,13 +229,18 @@ module.exports = React.createClass({
|
|||||||
if (!(filename in this.state.fileSizes)) {
|
if (!(filename in this.state.fileSizes)) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
utils.getFileSize(utils.getFileUrl(filename), function fileSizeOp(fileSize) {
|
Client.getFileInfo(
|
||||||
if (self.canSetState) {
|
filename,
|
||||||
var fileSizes = self.state.fileSizes;
|
function(data) {
|
||||||
fileSizes[filename] = fileSize;
|
if (self.canSetState) {
|
||||||
self.setState(fileSizes);
|
var fileSizes = self.state.fileSizes;
|
||||||
|
fileSizes[filename] = parseInt(data["size"], 10);
|
||||||
|
self.setState(fileSizes);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -831,6 +831,20 @@ module.exports.uploadFile = function(formData, success, error) {
|
|||||||
return request;
|
return request;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.getFileInfo = function(filename, success, error) {
|
||||||
|
$.ajax({
|
||||||
|
url: '/api/v1/files/get_info' + filename,
|
||||||
|
dataType: 'json',
|
||||||
|
contentType: 'application/json',
|
||||||
|
type: 'GET',
|
||||||
|
success: success,
|
||||||
|
error: function onError(xhr, status, err) {
|
||||||
|
var e = handleError('getFileInfo', xhr, status, err);
|
||||||
|
error(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.getPublicLink = function(data, success, error) {
|
module.exports.getPublicLink = function(data, success, error) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/api/v1/files/get_public_link',
|
url: '/api/v1/files/get_public_link',
|
||||||
|
|||||||
@@ -604,23 +604,6 @@ module.exports.splitFileLocation = function(fileLocation) {
|
|||||||
return {ext: ext, name: filename, path: filePath};
|
return {ext: ext, name: filename, path: filePath};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Asynchronously gets the size of a file by requesting its headers. If successful, it calls the
|
|
||||||
// provided callback with the file size in bytes as the argument.
|
|
||||||
module.exports.getFileSize = function(url, callback) {
|
|
||||||
var request = new XMLHttpRequest();
|
|
||||||
|
|
||||||
request.open('HEAD', url, true);
|
|
||||||
request.onreadystatechange = function onReadyStateChange() {
|
|
||||||
if (request.readyState === 4 && request.status === 200) {
|
|
||||||
if (callback) {
|
|
||||||
callback(parseInt(request.getResponseHeader('content-length'), 10));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
request.send();
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.toTitleCase = function(str) {
|
module.exports.toTitleCase = function(str) {
|
||||||
function doTitleCase(txt) {
|
function doTitleCase(txt) {
|
||||||
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user