Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Google Drive folder ignored on creation #73

Open
leorochael opened this issue May 8, 2015 · 5 comments
Open

Google Drive folder ignored on creation #73

leorochael opened this issue May 8, 2015 · 5 comments

Comments

@leorochael
Copy link

When creating MindMup file straight from Google Drive (i.e. from the red "New" button on the GDrive web interface), a new browser windows is created opening the URL:

https://www.mindmup.com/gd?state=...

Where the state variable above contains a URL encoded json structure like:

{'action': 'create',
 'userId': '[... Google User Unique Id ...]',
 'folderId': '[... Google Drive Folder Id ...]'}

For the same user, the folderId variable changes depending on which folder the user was navigating inside the GDrive interface, but the userId variable never changes.

However, after that, the user is redirected to the URL:

https://www.mindmup.com/#m:new-g

And even though MindMup remembers that the file should be saved on Google drive, it apparently loses the information of the specific GDrive folder, since the only result of clicking on the "Save" button (which is correctly selecting GDrive) is to save the MindMup file to the root folder of GDrive for the logged-in user,

The first time this happens to a GDrive user can be very confusing, considering that:

  • The file name is generated from the name of the central node
  • It won't be present in the folder where the user initially created the MindMup file
  • The icon in the GDrive root is not representative of MindMup: it's the standard "blue page" icon for every file of unknown format in GDrive.

Please consider preserving the original GDrive folder information (in a cookie or URL fragment) and using it to save the MindMup file in the proper location.

According to the documentation, saving a file to a specific folder is just a matter of passing the parents parameter to the POST request that creates the file.

@gojko
Copy link

gojko commented May 10, 2015

we're rewriting the drive integration at the moment, and one of the things on the list is to preserve folders. mindmup at the moment doesn't have a concept of folders, so there is nowhere to preserve the parents info. but it will be soon.

@leorochael
Copy link
Author

Since we're only interested in making the "New" button in GDrive work correctly, we have no need of dealing with multiple "parents" like the GDrive API allows. We only need to deal with one "parent", the "folderId" that was passed in state.

Considering that, once the file is created, MindMup can save to Google Drive directly with the file Id, you only need to preserve the folder Id in the original #m:new-g fragment, like:

#m:new-g:folderId=TheFolderIdFromGDrive

Then use that as a single entry in the parents parameter while creating the GDrive document.

After that, the parents information can be discarded.

So, all in all, only 2 very small changes are need for a massive improvement in usability with GDrive:

First, preserve the folderId parameter in the fragment when redirecting from www.mindmup.com/gd to www.mindmup.com.

In web.rb:84, replace:

    if !state || state['action']=='create' then
      mapid = "new-g"

With:

    if !state || state['action']=='create' then
      mapid = "new-g:folderId=" + state['folderId']

Then, use the information on the fragment to create the document.

In google-drive-adapter.js before the saveFile function in line 69, create the following function:

        toGoogleParents = function (mapId) {
            var match = /^new-g:folderId=(\w+)/.exec(mapId)
            if (match) {
                return [{ "kind": "drive#fileLink", "id": match[1] }];
            }
        },

And use it to create the parents parameter in the file metadata inside the saveFile function:

        saveFile = function (contentToSave, mapId, fileName, paramContentType) {
            var googleId =  toGoogleFileId(mapId),
                parents = toGoogleParents(mapId), // <-- Get the folderId from the fragment
                deferred = jQuery.Deferred(),
                boundary = '-------314159265358979323846',
                delimiter = '\r\n--' + boundary + '\r\n',
                closeDelim = '\r\n--' + boundary + '--',
                contentType = paramContentType || defaultContentType,
                metadata = {
                    'parents': parents ? parents : undefined, // <-- Add the FolderId information
                    'title': fileName,
                    'mimeType': contentType
                },

I stepped into the currently running MindMup version, and using the syntax above in the fragment, the folderId is correctly preserved inside mapId all the way into saveFile(). And I also managed to save to the correct folder by changing the metadata part of the request according to the info above.

I'd do a pull request, but I don't know enough about your codebase to write the proper tests to go along with these changes.

@leorochael
Copy link
Author

I updated the previous comment to fix the code and to report that I managed to save a new MindMup file to a specific GDrive folder with the information above by adding a breakpoint in the saveFile() function above.

@gojko
Copy link

gojko commented May 12, 2015

that's not going to work simply as that, because the next time you save the file, the information will be lost.

@leorochael
Copy link
Author

Hi @gojko, I understand your concern, but don't worry, it does work simply as that.

You're right that the folderId information is lost after the file is created, but by this point it doesn't matter: MindMup already has the file Id in GDrive, and that's all GDrive needs to update the file in place.

There is no need to pass the folder information when saving into an already existing file. For GDrive the folder information is just persistent file metadata, and if you don't specify it, GDrive won't touch it if it's already there.

As I mentioned, I tested this flow with the current version of MindMup by putting the folderId in the URL fragment for a new file and adding a breakpoint into the saveFile() function. There I used the JS console to add the parents information with a single value derived from the fragment into the metadata JSON part of the multiPart request to GDrive. MindMup saved the file correctly to the intended the GDrive folder.

After that, MindMup was redirected to it's usual URL for GDrive files:

`https://www.mindmup.com/#m:g1[...GDrive File Id...]`

Notice that the foldeId information was "lost", as you predicted, but if I change the file and save it, it doesn't move inside GDrive, it's changed in-place inside whatever folder it was saved.

If I close the browser window, then go into the GDrive folder where the file was saved and double-click on it, GDrive will open a new browser window with the exact same URL above, again with no folderId information. But if I change the file again and save, the file is correctly updated and doesn't move.

You can test yourself the fact that the folderId information is irrelevant after creation:

  • Create and save a MindMup file in GDrive the usual way
  • Close it
  • In GDrive, move it from the root, where it was created, to another GDrive folder
  • Open the file from inside the new folder
  • Change it and save it to your heart's content: it will stay put inside the GDrive folder where you placed it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants