简体   繁体   English

在实际保存文件之前,如何读取使用Cloud Code保存到Parse服务器的文件?

[英]How to read a file being saved to Parse server with Cloud Code, before actually saving it?

I'm trying to use Cloud Code to check whether a user-submitted image is in a supported file type and not too big. 我正在尝试使用Cloud Code来检查用户提交的图像是否在受支持的文件类型中并且不是太大。

I know I need to do this verification server-side and I think I should do it with Cloud Code using beforeSave – the doc even has a specific example about data validation, but it doesn't explain how to handle files and I couldn't figure it out. 我知道我需要在服务器端进行此验证,并且我想我应该使用beforeSave对Cloud Code进行验证-该文档甚至提供了有关数据验证的具体示例,但是它没有说明如何处理文件,因此我无法想办法。

I've tried the documented method for saving files, ie. 我尝试了记录文件保存的方法,即。

file = fileUploadControl.files[0];
var parseFile = new Parse.File(name, file);
currentUser.set("picture", parseFile);
currentUser.save();

and in the Cloud Code, Parse.Cloud.beforeSave(Parse.User, (request, response) => { // code here }); 在Cloud Code中, Parse.Cloud.beforeSave(Parse.User, (request, response) => { // code here });

But 1. this still actually saves the file on my server, right? 但是1.这实际上仍然将文件保存在我的服务器上,对吗? I want to check the file size first to avoid saving too many big files... 我想先检查文件大小,以避免保存太多大文件...

And 2. Even then, I don't know what to do in the beforeSave callback. 2.即使在那时,我仍然不知道在beforeSave回调中该怎么做。 It seems I can only access the URL of the saved image (proof that it has been uploaded), and it seems very counter-intuitive to have to do another https request to check the file size and type before deciding whether to proceed with attaching the file to the User object. 看来我只能访问已保存图像的URL(证明它已上传),并且在决定是否继续附加附件之前,必须执行另一个https请求来检查文件大小和类型似乎非常违反直觉。文件到用户对象。

(I'm currently using remote-file-size and file-type to check the size and type of the uploaded file, but no success here either). (我目前正在使用remote-file-sizefile-type来检查上载文件的大小和类型,但在这里也没有成功)。

I also tried calling a Cloud function, but it feels like I'm not doing the right thing, and besides I'm running into the same issues. 我还尝试了调用Cloud函数,但是感觉好像我做的不对,而且遇到了同样的问题。 I can call a Cloud function and pass a saved ParseFile as a parameter, and then I know how to save it to the User object from the Cloud Code using the masterkey, but as above it still involves uploading the file to the server and then re-fetching it using its URL. 我可以调用Cloud函数并将保存的ParseFile作为参数传递,然后我知道如何使用万能密钥将其从Cloud Code保存到User对象,但是如上所述,它仍然涉及将文件上传到服务器,然后重新-使用其网址获取。

Am I missing anything here? 我在这里想念什么吗? Is there no way to do something like a beforeSave on Parse.File, and then stop the file from being saved if it doesn't meet certain criteria? 没有办法在Parse.File上执行诸如beforeSave的操作,然后在不符合特定条件时停止保存文件?

Cheers. 干杯。

If you have to do something with files, parse lets you overwrite the file adapter to handle file operations. 如果您必须对文件进行某些操作,则使用parse可以覆盖文件适配器以处理文件操作。

You can indicate the file adapter to use in your ParseServer instatiation: 您可以指示要在ParseServer实例中使用的文件适配器:

    var FSStoreAdapter = require('./file_adapter');
    var api = new ParseServer({
      databaseURI: databaseUri ,
      cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
      appId: process.env.APP_ID,
      filesAdapter: fs_store_adapter, // YOUR FILE ADAPTER
      masterKey: process.env.MASTER_KEY, //Add your master key here. Keep it secret!
      serverURL: "https://yourUrl",  // Don't forget to change to https if needed
      publicServerURL: "https://yourUrl",
      liveQuery: {
        classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
      }
      maxUploadSize: "500mb"  //you will now have 500mb limit :)
    });

That said, you can also specify a maxUploadSize in your instatiation as you can see in the last line. 就是说,您还可以在实例中指定maxUploadSize,如最后一行所示。

you have to use save in background 您必须使用后台保存

file = ParseFile("filename", file)
file?.saveInBackground({ e ->
if (e == null) {

} else {
  Toast.makeText(applicationContext, "Error: $e", Toast.LENGTH_SHORT).show()

  e.printStackTrace()
  Log.d("DEBUG", "file " + e.code)
}
}, { percentDone ->
  Log.d("DEBUG", "file:" + percentDone!!)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM