question

Frank DiCola avatar image
Frank DiCola asked

Can I create & edit Entity files directly on the CloudScript server?

I'm trying to create a setup process for new accounts that use my app. On creating a new account, it's important to establish the default values for new users. One of those is a (mostly empty) Entity File called "CompletedQuests.json" that I'm using to save data.

I don't want to upload this file initially from the client in case the client isn't trustworthy or the app has been tampered with. So I was hoping to create a file on the server and place it among the 10 Entity files we're allowed to have. The code:

handlers.giveNewChariteerAccountDefaultEntityValues = function (args, context) {


    // Use this entity!
    var entityProfile = context.currentEntity;


    log.debug("Did this work? Trying to set a file...");


    entity.SetObjects({
        Entity: entityProfile.Entity,
        Objects: [
            {
                ObjectName: "TestObject",
                DataObject: {
                    item1: "This is Item 1",
                    item2: "This is Item 2"
                }
            }
        ]
    });


    var filename1 = "TestFile1";
    var filename2 = "TestFile2";


    // ** Here is where I would put data into filename1 and filename 2 **


    entity.InitiateFileUploads({
        Entity: entityProfile.Entity,
        Filenames: [
            {
                filename1,
                filename2
            }
        ]
    });


    return;
}

You can ignore the SetObjects call, that was just to make sure things were working. So that works, but I can't seem to figure out how to InitiateFileUploads.

Is there a specific server function I can use like SetFiles, similar to SetObjects? That would be convenient. The file is just JSON and when a new account is created, as I said, it barely has to have anything in it.

CompletedQuests.json

{
  "completedQuests":[]
}

That's all I would need! Now it was my understanding that you don't have to upload / finalize things on the server because it's the server but I guess I was wrong. I'm getting this error:

"result": null,
                    "apiError": {
                        "code": 400,
                        "status": "BadRequest",
                        "error": "InvalidParams",
                        "errorCode": 1000,
                        "errorMessage": "The resource Profile/Files/ was not valid. Error: Path must contain a value.",
                        "errorHash": null,
                        "errorDetails": null
                    }

Couldn't find any information about the error online so I wanted to ask. I'm finding EntityProfiles confusing, even more so since the rules for the server are different than rules for the client.

Thanks for the help!

CloudScriptentities
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

>> Is there a specific server function I can use like SetFiles, similar to SetObjects?

There is no such API that can modify the Entity Files’ content. And CloudScript cannot do the I/O operations, such as creating a file. If you only want to use JSON files to save players’ data, Entity Object can meet your requirements. As you said, it provides the API SetObjects to modify the content of it. With Entity Objects, you can store several JSON structured objects. And with Entity Files, you can store any size of arbitrary binary data.

If you still want to use Entity Files to store the JSON structured data, we would recommend you use CloudScript or custom servers to verify if the file‘s data is abnormal instead of creating and uploading the files.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Frank DiCola avatar image Frank DiCola commented ·

Thanks Sarah! I appreciate the quick reply. I decided I wanted to use Entity Files because I need a lot of storage and there's no limit. This is a list of Completed Quests and it will grow as a user uses the app, so I didn't want to hit a ceiling and be unable to save their accomplishments.

So it sounds like I'll have to verify the file with CloudScript, that's fine! I don't really need the server to create the file if it can verify the content. How can the CloudScript get access to the Entity Files on an entity (and the string of the file content itself)? I wasn't sure what function to use.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.