From c4015ffff442f4c379793df3193cad9699cd369c Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Tue, 14 May 2019 16:08:16 +0800 Subject: [PATCH 1/2] MDL-62403 portfolio: create directories for google drive export I've modified the Google Drive portfolio export so that it creates a root directory in Drive for all of the files in the export. This allows each export to be contained from each other to help with clashing file names. Additionally, the paths for the exported files will now be created in Drive so that exported files are in the correct directory structure. This is important for forum posts exports which assume a certain directory structure in the export. --- portfolio/googledocs/lib.php | 56 +++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/portfolio/googledocs/lib.php b/portfolio/googledocs/lib.php index 04733dda0f5..66fa8ecde7e 100644 --- a/portfolio/googledocs/lib.php +++ b/portfolio/googledocs/lib.php @@ -75,18 +75,66 @@ class portfolio_plugin_googledocs extends portfolio_plugin_push_base { throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); } + // Create a parent directory for the export to Google Drive so that all files from the + // same export can be contained in one place for easy downloading. + $now = time(); + $exportdirectoryname = $this->exporter->get('caller')->display_name(); + $exportdirectoryname = strtolower(join('-', explode(' ', $exportdirectoryname))); + $exportdirectoryname = "/portfolio-export-{$exportdirectoryname}-{$now}"; + $directoryids = []; + foreach ($this->exporter->get_tempfiles() as $file) { + $filepath = $exportdirectoryname . $file->get_filepath(); + $directories = array_filter(explode('/', $filepath), function($part) { + return !empty($part); + }); + + // Track how deep into the directory structure we are. This is the key + // we'll use to keep track of previously created directory ids. + $path = '/'; + // Track the parent directory so that we can look up it's id for creating + // subdirectories in Google Drive. + $parentpath = null; + + // Create each of the directories in Google Drive that we need. + foreach ($directories as $directory) { + // Update the current path for this file. + $path .= "${directory}/"; + + if (!isset($directoryids[$path])) { + // This directory hasn't been created yet so let's go ahead and create it. + $parents = !is_null($parentpath) ? [$directoryids[$parentpath]] : []; + try { + $filemetadata = new Google_Service_Drive_DriveFile([ + 'title' => $directory, + 'mimeType' => 'application/vnd.google-apps.folder', + 'parents' => $parents + ]); + + $drivefile = $this->service->files->insert($filemetadata, ['fields' => 'id']); + $directoryids[$path] = ['id' => $drivefile->id]; + } catch (Exception $e) { + throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $directory); + } + } + + $parentpath = $path; + } + try { // Create drivefile object and fill it with data. $drivefile = new Google_Service_Drive_DriveFile(); $drivefile->setTitle($file->get_filename()); $drivefile->setMimeType($file->get_mimetype()); + // Add the parent directory id to make sure the file gets created in the correct + // directory in Google Drive. + $drivefile->setParents([$directoryids[$filepath]]); $filecontent = $file->get_content(); - $createdfile = $this->service->files->insert($drivefile, - array('data' => $filecontent, - 'mimeType' => $file->get_mimetype(), - 'uploadType' => 'multipart')); + $this->service->files->insert($drivefile, + array('data' => $filecontent, + 'mimeType' => $file->get_mimetype(), + 'uploadType' => 'multipart')); } catch ( Exception $e ) { throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); } From 837651249b12c773eb705dd96a62d457bd36c44e Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 7 May 2020 12:38:35 +0800 Subject: [PATCH 2/2] MDL-62403 portfolio: add upgrade notes for google docs --- portfolio/googledocs/upgrade.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 portfolio/googledocs/upgrade.txt diff --git a/portfolio/googledocs/upgrade.txt b/portfolio/googledocs/upgrade.txt new file mode 100644 index 00000000000..a1c54cf9fc3 --- /dev/null +++ b/portfolio/googledocs/upgrade.txt @@ -0,0 +1,7 @@ +This files describes API changes in /portfolio/googledocs system, +information provided here is intended especially for developers. + +=== 3.8.3 === + +* The Google Drive portfolio exports are now bundled in a single parent folder. The paths for the exported files will + now be created in Drive so that exported files are in the correct directory structure.