elringus / unity-google-drive

Google Drive SDK for Unity game engine

Home Page:https://forum.unity.com/threads/515360

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[IOS] Uploading new file to shared folder using folder ID as parent uploads to the root of my drive instead, overwriting a file in the folder works though.

ROBYER1 opened this issue · comments

I followed all the setup for IOS/Android and Editor and this works fine in Editor on Mac/Windows but on IOS I am having this issue, I may test this on Android also soon.

Here is the sample code, I am saving a new file called "UserTest_1.json" if one doesn't already exist, otherwise I overwrite it. I then also save another json of the same file with the date added "UserTest_1_" + testDate + ".json", both of these files are meant to be saved to a Shared Folder, of which I have the folder ID and also check "sharedWithMe = true" when looking for the folder.

I use the params "Parents = new List { folderMainId } }" where the folder Id is a hard-coded variable, which I also debug.log out.

It seems when on IOS in a build, the correct stuff gets logged out e.g. with the ID omitted here of course for privacy

String wasn't empty, saving over file: at folder id: ###### , file id: ######

Then also

Saving dated user file at folder id: #####

But only the file saved over in the folder works and the new dated user file ends up in the root of my Google drive despite the logs telling me it was saved to the folder ID

The files that get saved in the root of my Gdrive when I try to create a new JSON file from IOS are just called 'Untitled' rather than the file name I set in UnityGoogleDrive.Data.File{ }.

public void testGetFile()
    {
        StartCoroutine(saveResultData());
    }

    private IEnumerator saveResultData()
    {
        //Convert results to bytes first, check if string is empty or not
        if (string.IsNullOrEmpty(debugOutputString))
        {
            debugOutputString = "Empty Test";
        }
        var bytes = System.Text.Encoding.UTF8.GetBytes(debugOutputString);

        //Continue

        if (!string.IsNullOrEmpty(fileMainId))
        {

            Debug.Log("String wasn't empty, saving over file: at folder id: " + folderMainId + ", file id: " + fileMainId);
            var file = new UnityGoogleDrive.Data.File { Name = "UserTest_1.json", Content = bytes };
            var request = GoogleDriveFiles.Update(fileMainId, file);
            yield return request.Send();
        }
        else
        {
            Debug.Log("String was empty, make new file at folder id: " + folderMainId);
            var file = new UnityGoogleDrive.Data.File { Name = "UserTest_1.json", Content = bytes, Parents = new List<String> { folderMainId } };
            GoogleDriveFiles.Create(file).Send();
        }

        //Create date timestamped version of the file also, remove any illegal characters
        string testDate = DateTime.Now.ToString();
        testDate = testDate.Replace(" ", "_");
        testDate = testDate.Replace("/", "_");
        testDate = testDate.Replace(":", "_");
        //Google drive test
        Debug.Log("Saving dated user file at folder id: " + folderMainId);
        var file2 = new UnityGoogleDrive.Data.File { Name = "UserTest_1_" + testDate + ".json", Content = bytes, Parents = new List<String> { folderMainId } };
        GoogleDriveFiles.Create(file2).Send();

    }

The closest thing I could find to this issue is this but I am not using a multipart upload I am simply uploading a small JSON file

I tested this some more, it doesn't matter if there is a folder that is shared with me or a folder on my local drive. The main thing is that the existing file I am overwriting does get overwritten, but the newly created files are just called 'Untitled' and are dumped into the root of my Google drive when uploading from IOS.

I found some related issues to this on Stack Overflow but nothing with an immediate fix from the fixes I tried
https://stackoverflow.com/questions/70005455/how-to-solve-the-file-upload-issue-by-using-google-drive-api
https://stackoverflow.com/questions/71273132/why-uploading-multi-files-to-google-drive-shows-untitled-file
https://stackoverflow.com/questions/63146534/filename-is-untitled-when-uploading-a-file-using-google-drive-api-node-js

I also found some evidence of this on android where people said that ProGuard was messing with the reflection calls but I am mainly seeing this issue on IOS
https://stackoverflow.com/questions/54604542/google-drive-v3-with-java-client-library-creates-untitled-files-on-android

I also tried this with the copy command on IOS, thinking if overwriting the file works then I could just make a copy of it with the file name I need, unfortunately this also resulted in 'Untitled' files being saved in the root of my Gdrive still. Works fine in Editor though!

I made sure to re-do all the setup steps for IOS/Android

Sample code:

    {
        var file = new UnityGoogleDrive.Data.File() { Id = fileMainId, Name = string.IsNullOrEmpty(fileNameNew) ? null : fileNameNew };
        copyRequest = GoogleDriveFiles.Copy(file);
        copyRequest.Fields = new List<string> { "name, size, createdTime" };
        copyRequest.Send().OnDone += BuildResultString;
    }

After further investigation, the test scripts worked in the project and I realised that I was using coroutines and not using the GoogleDriveFiles.CreateRequest for the file creation or sending which was causing files to arrive as 'Untitled' by name.

However, I can't get to the bottom of why the newly created files only arrive in the root folder of my drive. Here is my new sample, the files are now named correctly but the function CreateFile() puts the files in the root of my Gdrive on IOS, works fine in editor it puts them in the correct folder. The parent folder ID is hardcoded also.

private IEnumerator saveResultData()
    {
        overwriteFile();
        yield return new WaitForSecondsRealtime(1.0f);
        createFile();
        yield return null;
    }

    private void overwriteFile()
    {
        //Convert results to bytes first, check if string is empty or not
        if (string.IsNullOrEmpty(debugOutputString))
        {
            debugOutputString = "Empty Test";
        }
        bytes = System.Text.Encoding.UTF8.GetBytes(debugOutputString);

        //Continue

        if (!string.IsNullOrEmpty(fileMainId))
        {

            Debug.Log("String wasn't empty, saving over file: " + fileMainId);
            var file = new UnityGoogleDrive.Data.File { Name = "UserTest_1.json", Content = bytes };
            var request = GoogleDriveFiles.Update(fileMainId, file);
            request.Send();
        }
        else
        {
            Debug.Log("String was empty, make new file");
            var content = bytes;
            if (content == null) return;

            var file = new UnityGoogleDrive.Data.File() { Name = "UserTest_1.json", Content = content, Parents = new List<String> { folderMainId } };
            newRequest = GoogleDriveFiles.Create(file);
            newRequest.Fields = new List<string> { "id", "name", "size", "createdTime" };
            newRequest.Send();
        }
    }

    private void createFile()
    {
        //Create date timestamped version of the file also, remove any illegal characters
        string testDate = DateTime.Now.ToString();
        testDate = testDate.Replace(" ", "_");
        testDate = testDate.Replace("/", "_");
        testDate = testDate.Replace(":", "_");

        var content = bytes;
        if (content == null) return;

        var file = new UnityGoogleDrive.Data.File() { Name = "UserTest_1_" + testDate + ".json", Content = content, Parents = new List<String> { folderMainId } };
        file.Parents = new List<String> { folderMainId };
        request = GoogleDriveFiles.Create(file);
        request.Fields = new List<string> { "id", "name", "size", "createdTime" };
        request.Send();
    }

If I change createFile() code to this to copy a file it manages to copy the file in the correct folder from IOS, seems there may be an issue with creating a file in a specified folder on IOS that the test scripts/scenes don't cover - may be worth checking that!

Hope this helps anyone else who runs into this as the editor behaviour is more forgiving than on device it seems you cannot trust it just working in the editor before building to device

private GoogleDriveFiles.CopyRequest copyRequest;

    private void createFile()
    {
        //Create date timestamped version of the file also, remove any illegal characters
        string testDate = DateTime.Now.ToString();
        testDate = testDate.Replace(" ", "_");
        testDate = testDate.Replace("/", "_");
        testDate = testDate.Replace(":", "_");

        //Copying
        var file = new UnityGoogleDrive.Data.File() { Id = fileMainId, Name = "UserTest_1_" + testDate + ".json" };
        copyRequest = GoogleDriveFiles.Copy(file);
        copyRequest.Fields = new List<string> { "name, size, createdTime" };
        copyRequest.Send();

        Debug.Log("Copied file");
}

This issue is stale because it has been open 14 days with no activity. It will be automatically closed in 7 days.

After much investigation and cross-comparing with the sample scenes in this repo, I realised this issue was caused by Managed Stripping Level being higher than 'Low', I have updated the readme for it here in this PR. I hope this is useful to other people