when folder with same name is moved to trash API picks it up
Given I create folder "foo" And within the folder "foo" I create folder "2017" And within the folder "foo" I create another folder named "2017" And I move the first of the folders to trash Then when I list existing folders matching "2017" and upload to it It will upload to folder "2017" that is in trash
the code for uploading to the foo/2017/this-is-awesome-folder is something like this
def existing_gdrive_year_folder(year)
folder = gdrive.list_files({
q: "'#{foo_folder_id}' in parents",
page_size: 200,
fields: 'files(id, name)'
})
folder.files.find { |f| f.name == year }
end
def create_gdrive_year_folder(year)
year_file_metadata = {
parents: [foo_folder_id],
name: year,
mime_type: 'application/vnd.google-apps.folder'
}
gdrive.create_file(year_file_metadata, fields: 'id')
end
def foo_folder_id
"xxxxxxxxxxxxxxxxxxxxxxx"
end
year = '2017'
year_folder = existing_gdrive_year_folder(year) || create_gdrive_year_folder(year)
file_metadata = {
parents: [year_folder.id],
name: "this-is-awesome-folder",
mime_type: 'application/vnd.google-apps.folder'
}
folder = gdrive.create_file(file_metadata, fields: 'id')
but this is not the issue, the issue is that folders in "trash" are treated as real folders
I haven't tried to reproduce this, it doesn't seem to have much to do with this client library but could you maintain a list of trashed folder ids and select new ids that aren't known as in the trash?
Removing files via the UI and removing them via service.delete_file has different effects.
If you create 2 folders "2007" and delete one via the UI,
files = service.list_files
files.files.select { |file| file.name == "2007" }
returns two files.
However if you create 2 folders "2007" and delete one via service.delete_file,
files = service.list_files
files.files.select { |file| file.name == "2007" }
returns only the one, non-deleted file.
To handle scenarios where there are two, identically named files/folders, where one is deleted via the UI, you can either select by file id, or by running service.empty_file_trash.
You can also find out if a file is in the "Trash" folder by running:
file = service.get_file file.id, fields: "trashed"
file.trashed?
Hope this helps.