Repository: yasirkula/DownloadLinkGeneratorForGoogleDrive Branch: master Commit: 913277b8c818 Files: 7 Total size: 27.9 KB Directory structure: gitextract_rr1mc12y/ ├── LICENSE ├── OLD_VERSION_PHP/ │ ├── README.md │ ├── create.php │ ├── oauth2callback.php │ └── open.php ├── README.md └── index.html ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2017 Süleyman Yasir KULA Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: OLD_VERSION_PHP/README.md ================================================ These PHP files are not used anymore (by http://yasirkula.net/drive/downloadlinkgenerator/). This version used the PHP client of Drive API, whereas the new version uses Javascript client. ================================================ FILE: OLD_VERSION_PHP/create.php ================================================ action != 'create' ) { echo 'Invalid state'; $valid = 0; } else { $_SESSION['downloadlinkgenerator_createid'] = $state->folderId; } } if( $valid == 1 ) { $client = new Google_Client(); $client->setAuthConfig('../../../google-api-php-client-2.0.3/client_id.json'); $client->addScope('https://www.googleapis.com/auth/drive.install'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); if (isset($_SESSION['downloadlinkgenerator_accesstoken']) && $_SESSION['downloadlinkgenerator_accesstoken']) { if( !isset($_SESSION['downloadlinkgenerator_createid']) || !isset($_SESSION['downloadlinkgenerator_createid']) ) { echo 'Use the service with \'Create\' button in Drive UI'; } else { $client->setAccessToken($_SESSION['downloadlinkgenerator_accesstoken']); $drive_service = new Google_Service_Drive($client); $result = array(); GetFilesRecursively( $_SESSION['downloadlinkgenerator_createid'], '' ); echo '
';
foreach( $result as $fileInfo )
{
echo $fileInfo['path'] . ' ' . $fileInfo['link'] . "\r\n";
}
echo '';
}
}
else
{
$_SESSION['downloadlinkgenerator_op'] = 'create';
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
function GetFilesRecursively( $folderId, $relativePath )
{
global $result;
global $drive_service;
try
{
$pageToken = NULL;
do
{
$parameters = array(
'q' => "trashed=false and '" . $folderId . "' in parents and mimeType != 'application/vnd.google-apps.folder'",
'pageSize' => '1000',
'fields' => 'nextPageToken, files(name, webContentLink)' );
if ($pageToken)
{
$parameters['pageToken'] = $pageToken;
}
$files = $drive_service->files->listFiles($parameters);
$fileMetas = $files->getFiles();
foreach( $fileMetas as $meta )
{
if( $meta->webContentLink )
array_push( $result, [ "path" => $relativePath . $meta->name, "link" => $meta->webContentLink ] );
}
$pageToken = $files->getNextPageToken();
} while( $pageToken );
$pageToken = NULL;
do
{
$parameters = array(
'q' => "trashed=false and '" . $folderId . "' in parents and mimeType = 'application/vnd.google-apps.folder'",
'pageSize' => '1000',
'fields' => 'nextPageToken, files(id, name)' );
if ($pageToken)
{
$parameters['pageToken'] = $pageToken;
}
$folders = $drive_service->files->listFiles($parameters);
$folderMetas = $folders->getFiles();
foreach( $folderMetas as $meta )
{
GetFilesRecursively( $meta->id, $relativePath . $meta -> name . '\\' );
}
$pageToken = $folders->getNextPageToken();
} while( $pageToken );
}
catch (Google_Service_Exception $e)
{
if ($e->getCode() == 401)
{
unset($_SESSION['downloadlinkgenerator_accesstoken']);
$_SESSION['downloadlinkgenerator_op'] = 'create';
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
else if ($e->getCode() == 403)
{
if( ($e->getErrors()[0]["reason"] == "rateLimitExceeded"
|| $e->getErrors()[0]["reason"] == "userRateLimitExceeded"))
{
echo 'Try again in a minute.';
}
}
else
{
echo 'ERROR: ' . $e->getMessage();
}
}
}
?>
================================================
FILE: OLD_VERSION_PHP/oauth2callback.php
================================================
setAuthConfig('../../../google-api-php-client-2.0.3/client_id.json');
$client->setRedirectUri('http://yasirkula.net/drive/downloadlinkgenerator/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/drive.install');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['downloadlinkgenerator_accesstoken'] = $client->getAccessToken();
if (isset($_SESSION['downloadlinkgenerator_op']) && $_SESSION['downloadlinkgenerator_op'] == 'open')
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/open.php';
else
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/create.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
================================================
FILE: OLD_VERSION_PHP/open.php
================================================
action != 'open' )
{
echo 'Invalid state';
$valid = 0;
}
else
{
if( $state->ids )
$_SESSION['downloadlinkgenerator_openid'] = $state->ids[0];
else
{
echo 'A document created in Drive does not support direct download. You should first convert it to a downloadable format.';
$valid = 0;
}
}
}
if( $valid == 1 )
{
$client = new Google_Client();
$client->setAuthConfig('../../../google-api-php-client-2.0.3/client_id.json');
$client->addScope('https://www.googleapis.com/auth/drive.install');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
if (isset($_SESSION['downloadlinkgenerator_accesstoken']) && $_SESSION['downloadlinkgenerator_accesstoken'])
{
if( !isset($_SESSION['downloadlinkgenerator_openid']) || !isset($_SESSION['downloadlinkgenerator_openid']) )
{
echo 'Use the service with \'Open\' button in Drive UI';
}
else
{
$client->setAccessToken($_SESSION['downloadlinkgenerator_accesstoken']);
$drive_service = new Google_Service_Drive($client);
$result = array();
try
{
$fileMeta = $drive_service->files->get($_SESSION['downloadlinkgenerator_openid'], array( 'fields' => 'mimeType,webContentLink' ));
if( $fileMeta->mimeType == 'application/vnd.google-apps.folder' )
{
$_SESSION['downloadlinkgenerator_createid'] = $_SESSION['downloadlinkgenerator_openid'];
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/create.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
else
{
if( $fileMeta -> webContentLink )
echo $fileMeta -> webContentLink;
else
echo 'File is not shared';
}
}
catch (Google_Service_Exception $e)
{
if ($e->getCode() == 401)
{
unset($_SESSION['downloadlinkgenerator_accesstoken']);
$_SESSION['downloadlinkgenerator_op'] = 'open';
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
else if ($e->getCode() == 403)
{
if( ($e->getErrors()[0]["reason"] == "rateLimitExceeded"
|| $e->getErrors()[0]["reason"] == "userRateLimitExceeded"))
{
echo 'Try again in a minute.';
}
}
else
{
echo 'ERROR: ' . $e->getMessage();
}
}
}
}
else
{
$_SESSION['downloadlinkgenerator_op'] = 'open';
$redirect_uri = 'http://yasirkula.net/drive/downloadlinkgenerator/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
?>
================================================
FILE: README.md
================================================
# Download Link Generator For Google Drive™
This is the source code of *Download Link Generator For Drive™* extension: https://gsuite.google.com/marketplace/app/download_link_generator_for_drive/631283629814
It lets users generate a list of their files with their download links in a Google Drive™ folder. Feel free to use this repository as a reference if you are creating your own Google Drive™ extension with similar functionality.
**[GitHub Sponsors ☕](https://github.com/sponsors/yasirkula)**
## How does the extension work?
Using the [Google Drive API](https://developers.google.com/drive/api/v3/about-sdk), after user authenticates the extension, a number of queries are made as follows:
- If user opened a file with this extension, download link of that file is returned
- If user opened a folder with this extension, download links for all the files in that folder and any folders underneath it (recursive) are returned
## Why would I want to use this extension?
Say you have a large number of files on Google Drive™ and you want to get a download link for each of these files. The thing is, you don't want to spend so much time sharing each file one by one and copying their download links manually.
Instead, you can open the Drive™ folder that contains your files with this extension and the extension will generate a list of the download links in the following format (one file per line): `{File relative path} {File's download url}`
For these download links to work everywhere, it is sufficient to make the folder that contains your files public (to do this, you can right click the folder, select "*Get link*" and change visibility from "*Restricted*" to "*Anyone with the link*").
Be aware that downloading big files using a direct download link will prompt a Drive™ dialog stating that "*The file exceeds the maximum size that Google can scan.*" and the user must click the "*Download anyway*" button to proceed. In C#, you can skip this step using the following WebClient implementation: https://gist.github.com/yasirkula/d0ec0c07b138748e5feaecbd93b6223c
================================================
FILE: index.html
================================================
This Drive™ extension/add-on lets you generate direct download links for the files in your Drive™ storage. Simply right click the file/folder in your Drive™ and select Open with->Download Link Generator. When a folder is selected, download links for all the files in that folder are generated. If Download Link Generator button isn't present, then you may first need to authorize this extension by clicking the Authorize button below.
For the generated download links to work everywhere, you need to make the file/folder public. To do this, you can right click the file/folder, select Get link and change visibility from Restricted to Anyone with the link.
This extension accesses the metadata of the selected file/folder and reads its download link from that metadata. All communications with the Drive™ servers is handled via the official Drive™ Javascript API and your Drive™ data is not stored in any way in our databases. This extension's use of information received from Google APIs adheres to Google API Services User Data Policy, including the Limited Use requirements.
Regarding the "See information about your Google Drive files" permission asked during the authorization:
This extension is hosted at yasirkula.net website and is subject to its Privacy Policy.
Status: connecting to Drive™ servers, please wait... (If nothing happens, please make sure that pop-ups are enabled and try again since authorization is handled via a pop-up)