Jasper Duizendstra
Jasper Duizendstra

Reputation: 2617

Different original file name and title using the api or change the original filename

From the API documentation:

The original filename if the file was uploaded manually, or the original title if the file was inserted through the API. Note that renames of the title will not change the original filename. This will only be populated on files with content stored in Drive.

I would like to insert a file with a title and a different original file name. According to (my interpretation) of the documentation this would be a two step approach, first insert the document with the title that should become the original file name, and then perform a patch to change the title. This requires two steps to have a difference between the title and the original file name. It also means that the original file name cannot be changed using the API.

Am I missing something?, is there a better way to set the original file name?

Upvotes: 0

Views: 2229

Answers (2)

Ves Ten
Ves Ten

Reputation: 11

We were looking to keep the original file-name of the revision file being uploaded in the history list when the actual file in Google drive had a different name.

We wanted the Google drive file to be named with a static name and all the revisions in the history list to contain the date in their file-names.

There is not direct functionality available to do that through the Google Drive API.

The natural thing springing into mind was to update the revision's originalFilename field through the API (we are using NodeJS with module "googleapis" v3)

service.revisions.update(
    {
             fileId: <fileId>,
             revisionId: <revisionId>,
             resource: {
                  originalFilename : <revision file name>
             }
    }
)

But the response was :

{
      Error: The resource body includes fields which are not directly writable.
      code: 403,  
      errors:
      { 
           domain: 'global',
           reason: 'fieldNotWritable',
           message: 'The resource body includes fields which are not directly writable.'
      } 
}

So it turned out the originalFilename field was recognizable on revisions.update but somehow not updatable - may be done like that on purpose for some reason.

The workaround that we used which actually managed to achieve our initial goal was to programmatically :

  1. Rename the google drive file, before uploading the revision, to the revision's file name
  2. Upload the revision to the google drive ( now the originalFilename of the revision is being automatically set to the actual file name which we conveniently renamed on the previous step to be the revisions file name )
  3. Rename the google drive file to its original file name

So if we have google drive file named "GooleDriveFile.dat" and we want to upload revision named GooleDriveFile_01012016.dat and to keep the original revision name in the history list we can programmatically :

1-> rename the google drive file from GooleDriveFile.dat to GooleDriveFile_01012016.dat

service.files.update(
      {
          fileId : <fileId>,
          resource: {
               name: "GooleDriveFile_01012016.dat",
          },
          fields :  "id,headRevisionId"
      } 
) 

2-> Upload the revision to the google drive

service.files.update(
   {
        fileId : <fileid>,
        newRevision : true,
        keepRevisionForever : true,
        media: {
            body: fs.createReadStream(<local path to the revision file>)  
        },
        fields : "id,headRevisionId"
    }
)

3-> Rename the google drive file back to "GooleDriveFile.dat"

service.files.update(
      {
          fileId : <fileId>,
          resource: {
               name: "GooleDriveFile.dat",
          },
          fields :  "id,headRevisionId"
      } 
) 

Upvotes: 1

Jay Lee
Jay Lee

Reputation: 13528

Actually, I've not been able to get originalFilename to return even when doing a insert() then patch(). When the documentation says "if the file was uploaded manually" I believe it's referring to the user uploading it via the Drive Web UI.

You might be better off recording the original file name in some other attribute like indexableText or description.

Upvotes: 0

Related Questions