Filepicker.io with Meteor:如何 return 实际资产而不是 link 资产
Filepicker.io with Meteor: how to return an actual asset rather than a link to the asset
我将 filepicker.io 与 Meteor 应用程序一起使用,并且正在为我的应用程序处理安全问题。 Filepicker 支持创建和签署策略,但是对于 Meteor 的服务器端,我觉得为每个请求文件的用户创建过期策略是多余的。
我想做的是为用户提供一个间接 link 文件。服务器使用服务器端路由(iron-router)拦截此请求,然后服务器通过包含有关该文件的元数据的文件集合检查用户是否具有该文件的权限。
正如我现在拥有的那样,如果用户有访问权限,我会向他们提供一个文件 link,其中包含签名和策略作为 link 的参数。相反,我宁愿只 return 图像或文件资产,而根本没有 link。例如。服务器端将通过只有服务器知道的 link 访问文件或图像,但服务器会将该文件或图像流式传输到客户端,而不会将实际的 link 共享到文件。
预期的代码如下所示,我真的不知道最后该做什么:
@route "file",
path: "/file/:_id"
where: "server"
action: ->
if @request.cookies.meteor_login_token
user = Meteor.users.findOne(
{"services.resume.loginTokens.hashedToken":
Accounts._hashLoginToken(@request.cookies.meteor_login_token)}
)
if user
# the files collection has metadata about each file
# these files are uploaded through filepicker
# this include file.url which is the actual link
file = share.Files.findOne(
{_id: @params._id, accessibleBy: user._id}
)
if not file
@response.writeHead(403)
@response.end()
return
else
#return the file/image from filepicker,
#e.g. file.url without actually returning the url
@response.end()
在我的研究中,似乎 stream 可能是解决方案,但对我来说,我如何在 node.js 光纤中做到这一点并不明显,就像在 Meteor 服务器上的情况一样-边。
您可以使用 webapp
to create an HTTP endpoint, then use node's http
从 Filepicker
获取图像,然后将其通过管道传输到响应:
var http = Npm.require('http'),
url = Npm.require('url');
WebApp.connectHandlers.use('/file', function(req, res, next) {
// URL format: /file/:_id
var id = url.parse(req.url, true).path.substr(1); // this is _id
// replace the following parameters with filepicker stuff
http.request({
host: 'www.devbattles.com',
path: '/en/images/upload/1427871558.png'
}, function(result){
// here we just pipe the http result
res.writeHead(200, {
'Content-Type': result.headers['content-type']
});
result.pipe(res);
}).end();
});
如果您将此代码(例如)复制到 /server/filepickerPipe.js
,然后打开 http://server.com/file/test
,您会看到 this picture。
旁注:
有可能通过 DDP 提供所有这些服务。我还必须开始从第 3 方提供文件,所以我可能会研究它并创建一个包来通过 DDP 来完成它而不是与 HTTP 端点混淆。但是,这是目前的解决方案。
我将 filepicker.io 与 Meteor 应用程序一起使用,并且正在为我的应用程序处理安全问题。 Filepicker 支持创建和签署策略,但是对于 Meteor 的服务器端,我觉得为每个请求文件的用户创建过期策略是多余的。
我想做的是为用户提供一个间接 link 文件。服务器使用服务器端路由(iron-router)拦截此请求,然后服务器通过包含有关该文件的元数据的文件集合检查用户是否具有该文件的权限。
正如我现在拥有的那样,如果用户有访问权限,我会向他们提供一个文件 link,其中包含签名和策略作为 link 的参数。相反,我宁愿只 return 图像或文件资产,而根本没有 link。例如。服务器端将通过只有服务器知道的 link 访问文件或图像,但服务器会将该文件或图像流式传输到客户端,而不会将实际的 link 共享到文件。
预期的代码如下所示,我真的不知道最后该做什么:
@route "file",
path: "/file/:_id"
where: "server"
action: ->
if @request.cookies.meteor_login_token
user = Meteor.users.findOne(
{"services.resume.loginTokens.hashedToken":
Accounts._hashLoginToken(@request.cookies.meteor_login_token)}
)
if user
# the files collection has metadata about each file
# these files are uploaded through filepicker
# this include file.url which is the actual link
file = share.Files.findOne(
{_id: @params._id, accessibleBy: user._id}
)
if not file
@response.writeHead(403)
@response.end()
return
else
#return the file/image from filepicker,
#e.g. file.url without actually returning the url
@response.end()
在我的研究中,似乎 stream 可能是解决方案,但对我来说,我如何在 node.js 光纤中做到这一点并不明显,就像在 Meteor 服务器上的情况一样-边。
您可以使用 webapp
to create an HTTP endpoint, then use node's http
从 Filepicker
获取图像,然后将其通过管道传输到响应:
var http = Npm.require('http'),
url = Npm.require('url');
WebApp.connectHandlers.use('/file', function(req, res, next) {
// URL format: /file/:_id
var id = url.parse(req.url, true).path.substr(1); // this is _id
// replace the following parameters with filepicker stuff
http.request({
host: 'www.devbattles.com',
path: '/en/images/upload/1427871558.png'
}, function(result){
// here we just pipe the http result
res.writeHead(200, {
'Content-Type': result.headers['content-type']
});
result.pipe(res);
}).end();
});
如果您将此代码(例如)复制到 /server/filepickerPipe.js
,然后打开 http://server.com/file/test
,您会看到 this picture。
旁注:
有可能通过 DDP 提供所有这些服务。我还必须开始从第 3 方提供文件,所以我可能会研究它并创建一个包来通过 DDP 来完成它而不是与 HTTP 端点混淆。但是,这是目前的解决方案。