The insertPermission
function, located in a Node.js module, takes a file ID and a user's email address as input and returns a promise that resolves to the ID of a newly created permission. The function authorizes access to Google Drive, creates a new permission on the specified file, and extracts the permission ID from the response before exporting it as a module for use elsewhere.
npm run import -- "insert google drive permissions"
var util = require('util');
var importer = require('../Core');
var authorizeDrive = importer.import("authorize google drive");
function insertPermission(fileId, email) {
return authorizeDrive()
.then(drive => util.promisify(drive.permissions.create.bind(drive))({
resource: {
'type': 'user',
'role': 'owner',
'emailAddress': email
},
fileId: fileId,
fields: 'id',
transferOwnership: true
}))
.then(r => r.data.id)
}
module.exports = insertPermission;
// Import required modules
const { google } = require('googleapis');
const { authorizeGoogleDrive } = require('../Core');
/**
* Inserts permission for a file in Google Drive
*
* @param {string} fileId - ID of the file
* @param {string} email - Email address of the user
* @returns {Promise<string>} ID of the newly created permission
*/
async function insertPermission(fileId, email) {
// Authorize Google Drive using the authorized client
const drive = await authorizeGoogleDrive();
// Create a permission for the user as an owner
const permission = await drive.permissions.create({
requestBody: {
type: 'user',
role: 'owner',
emailAddress: email
},
fileId,
fields: 'id',
transferOwnership: true
});
// Return the ID of the newly created permission
return permission.data.id;
}
// Export the insertPermission function
module.exports = insertPermission;
The code starts by importing two modules:
util
: a built-in Node.js module that provides utility functions for working with promises.importer
: a custom module located in the ../Core
directory, which is used to import specific functions from other modules.authorizeDrive
: a function imported from the previously imported importer
module, which authorizes access to Google Drive.insertPermission
FunctionThe insertPermission
function takes two arguments:
fileId
: the ID of a Google Drive file.email
: the email address of the user to be given permission to the file.The function returns a promise that resolves to the ID of the newly created permission.
Here's a step-by-step breakdown of the function:
authorizeDrive()
is called to authorize access to Google Drive. This returns a promise that resolves to the authorized Drive object.promisify
function from the util
module is used to convert the permissions.create
method of the Drive object into a promises-based function. This method creates a new permission on the specified file.permissions.create
function is called with the following arguments:
resource
: an object containing the permission settings, including the user's email address, role, and type.fileId
: the ID of the file to which the permission should be granted.fields
: a string specifying which fields should be returned in the response (in this case, only the id
field).transferOwnership
: a boolean indicating whether the ownership of the file should be transferred to the specified user (set to true
in this case).permissions.create
function is awaited and the id
field of the response is extracted.insertPermission
FunctionFinally, the insertPermission
function is exported as a module, making it available for use in other parts of the application.