Google Drive: File; Copy

Google Drive: File; Copy

Creates a copy of the specified file in the Drive. Supports both Google Doc files and common files. File ID can be obtained from URI etc. (If Folder-ID not specified, the file will inherit any discoverable parents of the source file.)

2019-09-20 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-copy/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set FILE-ID to Download * #{EL}
  • C: Set FOLDER-ID of Drive which contain New File #{EL}
  • D: Set FILE NAME if you want to save as a different name #{EL}
  • E: Select STRING DATA for New File ID (update)
  • F: Select STRING DATA for New File Name (update)
  • G: Select STRING DATA for File MimeType (update)
  • H: Select STRING DATA for URL for Editing (update)
  • I: Select STRING DATA for URL for Viewing (update)
Script
// (c) 2019, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
//  "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force"
// Token Endpoint URL:
//  "https://accounts.google.com/o/oauth2/token"
// Scope:
//  "https://www.googleapis.com/auth/drive"
//  'drive.file' only gives you permission to files that your app has created
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2  = configs.get( "conf_OAuth2"  ) + "";
const fileId  = configs.get( "conf_FileId") + "";
const folderId  = configs.get( "conf_FolderId") + "";
let   saveAs  = configs.get( "conf_SaveAs") + "";
const dataIdE = configs.get( "conf_DataIdE" ) + "";
const dataIdF = configs.get( "conf_DataIdF" ) + "";
const dataIdG = configs.get( "conf_DataIdG" ) + "";
const dataIdH = configs.get( "conf_DataIdH" ) + "";
const dataIdI = configs.get( "conf_DataIdI" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
engine.log( " AutomatedTask Config: File ID: " + fileId );
if( fileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {B} (File ID) is required \n" );
}
engine.log( " AutomatedTask Config: Folder ID: " + folderId );
if( folderId === "" ){
  engine.log( "  (File will be placed directly in My Drive folder.)" );
}
engine.log( " AutomatedTask Config: Save As: " + saveAs );


//// == Data Retrieving / ワークフローデータの参照 ==
// nothing


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// post Copy Request
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.queryParam( "supportsAllDrives", "true" );
// This parameter will only be effective until June 1, 2020.
// see https://developers.google.com/drive/api/v3/reference/files/copy
if( folderId !== "" || saveAs !== "" ){
  let requestObj = {};
  if( folderId !== "" ){
    requestObj.parents = [];
    requestObj.parents[0] = folderId;
  }
  if( saveAs !== "" ){
    requestObj.name = saveAs;
  }
  apiRequest = apiRequest.body( JSON.stringify( requestObj ), "application/json" );
}
const apiUri = "https://www.googleapis.com/drive/v3/files/" + fileId + "/copy";
engine.log( " AutomatedTask Trying: POST " + apiUri );
const response = apiRequest.post( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
engine.log( " AutomatedTask ApiResponse: New File ID: " + responseObj.id );
engine.log( " AutomatedTask ApiResponse: File MIME Type: " + responseObj.mimeType );


//// == Data Updating / ワークフローデータへの代入 ==
if( dataIdE !== "" ){
  engine.setDataByNumber( dataIdE, responseObj.id );
}
if( dataIdF !== "" ){
  engine.setDataByNumber( dataIdF, responseObj.name );
}
if( dataIdG !== "" ){
  engine.setDataByNumber( dataIdG, responseObj.mimeType );
}
if( dataIdH !== "" ){
  engine.setDataByNumber( dataIdH, "https://docs.google.com/open?id=" + responseObj.id );
}
if( dataIdI !== "" ){
  engine.setDataByNumber( dataIdI, "https://drive.google.com/file/d/"+ responseObj.id + "/view" );
}

} //////// END "main()" ////////

Download

Capture

Notes

  1. No way to save a copy to “My Drive”

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: