将URL更改为文件(使用Javascript CloudCode的Parse.com)

时间:2015-08-03 08:15:14

标签: javascript http parse-platform cloud-code

我需要将一些图像链接(一个类中存在的URL链接)批量更改为图像文件(Parse.com托管)。

云代码(显然)是如何做到的。

我已按照文档here进行了操作,但没有取得任何成功。

我想做的是:

  1. 从“COLUMN_1”
  2. 获取网址链接
  3. 将其设为文件
  4. 将文件上传到“COLUMN_1”(覆盖现有网址)。如果这很危险 - 可以将其上传到新列(“COLUMN_2”)。
  5. 重复下一行
  6. 此代码不起作用(这是我第一次使用JS):

    imgFile.save().then(function () {
        object.set("COLUMN_1", imgFile);
        return object.save();
    }).then(function (CLASSNAME) {
        response.success("saved object");
    }, function (error) {
       response.error("failed to save object");
    });
    

    有人可以推荐怎么做吗?

1 个答案:

答案 0 :(得分:0)

好的 - 这对其他人来说都是成功的。

    Parse.Cloud.job("convertFiles", function(request, status) { //Cuts the rundata out of poor runs
    function sleep(milliseconds) {
      var start = new Date().getTime();
      for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
          break;
        }
      }
    }

    // Tell the JS cloud code to keep a log of where it's upto. Manually create one row (in class "debugclass") to get an object Id
    Parse.Cloud.useMasterKey();

    var Debug = Parse.Object.extend("debugclass");
    var queryForDebugObj = new Parse.Query(Debug);
    queryForDebugObj.equalTo("objectId", "KbwwDV2S57");

    // Query for all users
    // var queryForSublist = new Parse.Query(Parse.Object.extend("gentest"));


    queryForDebugObj.find({
        success: function(results) {
            var debugObj = results[0];

            var processCallback = function(res) {
                    var entry = res[0];
                    var debugObj = results[0];
                    debugObj.set("LastObject", entry.id);
                    debugObj.save();

                    Parse.Cloud.httpRequest({
                        url: entry.get("smallImage2"),
                        method: "GET",
                        success: function(httpImgFile) 
                        {
                            console.log("httpImgFile: " + String(httpImgFile.buffer));
                            var imgFile = new Parse.File("picture.jpg", {base64: httpImgFile.buffer.toString('base64')});

                            imgFile.save().then(function () {
                                console.log("2");
                                entry.set("smallImage1", imgFile);
                                entry.save(null, {
                                  success: function(unused) {
                                    debugObj.increment("itemDone");
                                    sleep(20);
                                    res.shift();
                                    if (res.length === 0) {
                                        process(entry.id);
                                        return;
                                    }
                                    else {
                                        processCallback(res);
                                    }


                                  },
                                  error: function(unused, error) {
                                    response.error("failed to save entry");
                                  }
                                });    
                            });                                             
                        },
                        error: function(httpResponse) 
                        {
                            console.log("unsuccessful http request");
                            response.error(responseString);
                        }
                    });
            };
            var process = function(skip) {{
                var queryForSublist = new Parse.Query("genpants");

                if (skip) {
                    queryForSublist.greaterThan("objectId", skip);
                    console.error("last object retrieved:" + skip);
                }
                queryForSublist.ascending("objectId");
                queryForSublist.find().then(function querySuccess(res) {
                    processCallback(res);
                }, function queryFailed(reason) {
                    status.error("query unsuccessful, length of result " + result.length + ", error:" + error.code + " " + error.message);
                });
            }};

            process(debugObj.get("LastObject"));

        },
        error: function(error) {
            status.error("xxx Uh oh, something went wrong 2:" + error + "  " + error.message);
        }
    });  
});