gmail-processor icon indicating copy to clipboard operation
gmail-processor copied to clipboard

Existing files are not overwritten on Gdrive

Open soetheman opened this issue 5 years ago • 4 comments

Existing files are not overwritten on Gdrive. This symptom has been reported in issue#19 previously.

  • How to implement the suggested code changes using "file.clear() to remove all the contents of the old file, and then file.append() to insert all of the new contents."?
  • How to implement the suggested code changes using "file.setContent"?

For the time being we are added the following piece in Gmail2Gdrive of code (using the id of the containing folder): // ====== Remove existing renamed file ======= Added by Soe ======= // --- Delete has to be performed using file id: therefore first convert the actual file name to its file id --- var file = {title: newFilename, mimeType: 'image/jpeg'}; var myFolder = DriveApp.getFolderById('1F0orpMztVFc3jfCt8HMcT-UuK7tzIvRb'); // myFolder id using key of Graphics subfolder var allFilesByName = myFolder.getFilesByName(newFilename); // --- Remove the previous existing renamed file --- while (allFilesByName.hasNext()) { var thisFile = allFilesByName.next(); var theFileID = thisFile.getId(); var rtrnFromDLET = Drive.Files.remove(theFileID); Logger.log('Msg005a Deleted the existing renamed file "' + thisFile + '", ID "' + theFileID + '", rtrnFromDLET: ' + rtrnFromDLET); }; // ==== Existing renamed file removed ============

    var file = folder.createFile(attachment);  // duplicate previous statement (~ #85) to redefine the 'file' var
    attachment.getName()
    Logger.log("Msg005b:   Renaming '" + file.getName() + "' -> '" + newFilename + "'");
    file.setName(newFilename);

// ========= Remove the stored attachment file ===== Added by Soe ======= var allFilesByName = myFolder.getFilesByName(attachment.getName()); while (allFilesByName.hasNext()) { var thisFile = allFilesByName.next(); var theFileID = thisFile.getId(); var rtrnFromDLET = Drive.Files.remove(theFileID); Logger.log('Msg005c Deleted the stored attachment file "' + thisFile + '", ID "' + theFileID + '", rtrnFromDLET: ' + rtrnFromDLET); }; // =========== Stored attachment file removed =========

Thanks for your help, Regards, Paul,

soetheman avatar Apr 30 '19 14:04 soetheman

Hi @soetheman,

I am interested in how you have implemented this, as this would solve an issue I have too. When uploading files with the same name via the web UI you can choose to upload a duplicate or add a version (this would be a nice feature to add to gmail2drive), but in the interim if it could just remove any files with the same name and add the new version, that would be great.

Could you explain how you have added the code (placement in the script, etc.) provided please?

Thanks, Matt

matt-jon35 avatar Jun 23 '22 10:06 matt-jon35

@matt-jon35 I'm not sure but maybe handling different revisions requires to use the advanced Drive API: https://developers.google.com/drive/api/v3/reference/revisions Here's a StackOverflow question that deals with that issue in the context of Google Apps Script: https://stackoverflow.com/questions/47684835/google-apps-script-setting-revision-history Maybe that helps ...

ahochsteger avatar Jun 23 '22 11:06 ahochsteger

Thanks. Is v2 being developed?

matt-jon35 avatar Aug 04 '22 13:08 matt-jon35

@matt-jon35 yes, I've started a complete re-write using a state-of-the-art development setup (NPM dependency management, test automation, Typescript, ...) in the v2 branch, but it is not yet in a working state: https://github.com/ahochsteger/gmail2gdrive/tree/v2 When it's finished, most (if not all) open issues should be adressed.

ahochsteger avatar Aug 15 '22 10:08 ahochsteger

@ahochsteger Is there any update on this feature yet, please? Thanks for your work on this - it's incredibly useful to us and we would be lost without it!

mattmickeyj avatar Aug 01 '23 10:08 mattmickeyj

@mattmickeyj, thanks for your kind words, they are well appreciated :-).

In fact the code for v2 is almost done, it just needs more testing, polishing and documentation to make the first release.

I'd be more than happy if you'd like try the new implementation and give me some feedback. There's now a dry-run mode, that doesn't touch anything (neither GMail nor GDrive) but produces a log that shows what would have been done. This way the new version can be tested without risk of data-loss. You should even see, if an existing file would have been overwritten. For your use case have a look at ConflictStrategy argument of the attachment.store action.

Here are some pointers, in case you'd like to give it a try:

ahochsteger avatar Aug 03 '23 06:08 ahochsteger

@ahochsteger That's amazing, thank you very much for replying.

I would love to give it a go. I have just tried adding the library to a blank project, but get the following error:

Unable to look up library. Check the ID and access permissions and try again.

Thanks in advance.

mattmickeyj avatar Aug 03 '23 10:08 mattmickeyj

@mattmickeyj, which Script ID have you used to add the library?

ahochsteger avatar Aug 03 '23 14:08 ahochsteger

@mattmickeyj, I've changed the sharing options of the library, maybe you can try again?

ahochsteger avatar Aug 03 '23 14:08 ahochsteger

@ahochsteger That's worked - thank you.

Now to get my head around the new JSON rule formatting :-/

mattmickeyj avatar Aug 03 '23 14:08 mattmickeyj

@mattmickeyj You can automatically convert the old format into the new one - have a look at this example: https://github.com/ahochsteger/gmail2gdrive/blob/v2/src/gas/examples/v1-example01.js

ahochsteger avatar Aug 03 '23 14:08 ahochsteger

@ahochsteger Thank you. I feel like I am a bit out of my depths here with my understanding. I think I understand the JSON formatting now, but I was hoping to see the example code to see how to run it, but the file is missing:

https://github.com/ahochsteger/gmail2gdrive/blob/v2/docs/Code.gs

Sorry, I know you are not my 1-1 coach (haha!!), but any help is very much appreciated!!

mattmickeyj avatar Aug 04 '23 11:08 mattmickeyj

@mattmickeyj that's no problem - for me your questions are valuable feedback to fill in the missing pieces in the documentation until it's ready to be released. :-)

The code with the processing logic is now fully encapsulated in a shared library which you already added using the name GMail2GDrive I guess. So everything you need on your side within Code.gs is to define the configuration and a function to do the processing which looks like this:

var config = {
  // My (v2) config comes here ...
}

function run() {
  GMail2GDrive.Lib.run(config, "dry-run")
}

And then execute the function run in Google Apps Script.

If you want to convert the old v1 config to the new format you can do it this way and execute the function convertConfig in Google Apps Script:

var oldConfig = {
  // My old v1 config comes here ...
}

function convertConfig() {
  const config = GMail2GDrive.Lib.convertV1ConfigToV2Config(oldConfig)
  console.log(JSON.stringify(config, null, 2))
}

Note though, that the logging size is limited to about 8kB (or about 100kB if using StackDriver), so you may have to limit the number of converted rules that can be printed to the log.

I hope this clarifies a bit how the library can be used to run and convert the old configuration. These examples may help to see additional usages.

ahochsteger avatar Aug 04 '23 13:08 ahochsteger

I'm closing this issue since GMail2GDrive is discontinued and has been superseded by the much improved Gmail Processor.

See the Getting Started Guide as well as the Reference Docs for more information

ahochsteger avatar Sep 20 '23 20:09 ahochsteger