Serve Gmail Attachments with Content Service - google-apps-script

I'm using Google Apps Script and Content Service. I would like to provider users with a way to download an attachment for a given Gmail Message.
The Gmail Attachment objects doesn't expose a download URL like the Site Attachment does so I'm trying to use Content Service to serve the bits for the attachment. Content Service appears to only support text and does not have an API for returning byte[].
Is there a work around?

Option 1
You can encode your attachments as Base64 using Utilities.base64Encode() and then decode it at the client end.
Option 2
Save the attachment to Drive, give appropriate permissions and give the link to download the attachment.
As an aside, ContentService is useful only if your attachments are not accessed manually i.e. they are accessed through another program. If your users are manuaaly invoking the script, they might as well be given a link to download the attachment

Related

How to use preview image or PDF from Google Drive, using API?

I'm writting Web application to display content of Google Drive images and files, using API.
Currently, I can only see thumbnails of images/files (without login to Google drive).
If I want to preview the file, I need to be logged into Google drive and then I can use link returned by "webViewLink" and actually see the file.
I know I can click on folder or file on Drive and Share it, but I'm afraid that my customers will not be able to do that and it is complicated, anyway.
I already displaying Google dialog to customer where customer need to allow access to upload,delete etc. of files and now he can not preview the file???
Application is designed to display image/whatever to customer only, inside app only and not to sharing. With other words, I want to display images which he can see anyway if he is logged to Drive.
Is there any other option to allow customer to preview the file, if he already allowed full access previously?
Thanks.
Authorizing with OAuth does not automatically log you in. Users use their credentials to give their permission to create an access token, which needs to be used in any API calls. It does not imply that a browser session was created, that's a separate process.
You'll notice that the webViewLink is just the regular Drive URL with /view at the end. It's a page that requires the user to be signed in:
"webViewLink": "https://drive.google.com/file/d/<FILE-ID>/view?usp=drivesdk",
I'm not aware of any methods to sign in the user at the same time they use OAuth, but if you send your access_token in an Authorization: Bearer <access_token> header when trying to access the above URL you can see the preview without having to sign in. Depending on your platform I think implementing this would be tricky, and maybe not possible in Apps Script alone.
My recommendation as a workaround is to just use a full thumbnail. Don't know if you're aware of this, but the thumbnail URL has a =s parameter at the end that defines its height in pixels:
"thumbnailLink": "https://lh5.googleusercontent.com/<THUMB-ID>=s220",
You can change the default =s220 at the end to a higher size or remove it completely to get pretty much the full size of the image or PDF page. This may be enough for your users to figure out what the file is.

Intercept or get callbacks on Google Drive file save and open in addon/apps script

I want to build and AddOn for Google Drive that does extra pre-processing/decoration of files when they are uploaded and before opened/downloaded. E.g. add/verify digital signature, call 3rd party service passing file metadata, convert to different mime type, encrypt/decrypt certain files with custom key that's generated by 3rd party service.
I looked through all APIs and don't find any feature that allows to intercept file uploads/downloads and do pre-processing (e.g. intercept uploaded content, reading it as blob, do processing, then save it to Google Drive folder, later when user downloads file, do pre-processing/verification again on file contents and return end result) in a way that's transparent to user.
Another possibility in case intercepting the content would be to try intercept the file selection/upload event and change file extension or path to temp folder, then triggering processing script and saving result to original destination.
I prefer to avoid proxying file uploads/downloads via my custom backend, doing all required processing in context of addon/apps script.
I got a bit confused of how AppsScript GSUite Drive API (File, Folder, Drive classes) relates to Google Drive v2 API with Changes.watch(), Channels, etc...
Is latter intended for backend use only?
I can't see any notion of events/callbacks provided by AppsScript API, that in theory should be running my addon in context of current user.
I wish I would be abler to ask more concrete question here on API, but currently I struggling to understand how to do basic hooks for my addon to operate on files.
You can't intercept them. With a Drive Add-on you can only trigger functions when the user enters Drive's homepage or when the user select a file(s). With the API you can watch when a file is modified in order to get a notification, but this will be after the change is made without intercepting the change process.
If you use a OnItemsSelectedTrigger, you need to prompt/return a Card when the user selects a file(s), in this card you can set buttons that when clicked by the user it can trigger different actions.

Script to automatically open URL contained in mail

I am looking for a way to automatically open in chrome an URL link contained in specific mails, as soon as they arrive.
Those mails always come from the same address mail (abc#xyz.com), with a similar title, and they contain only one URL link with the same beginning: " http://www.abcd.com/..."
I'm using the app mail on a mac.
I created a rule in mail:
If a New mail arrives and the address contains "abc#xyz.com"
Then run Script "XXX".
I now can't seem to find a way to make this script work. I could use AppleScript ou JavaScript, but I was told Javascript will be better for this purpose. What do you think?

Open window and send POST data

I'm building an Add-on that needs to open a window and send some for data (via POST) to a remote server. I can do either of those items fine - UrlFetchApp() for sending the data, and an anchor tag to open a new window, but I couldn't find a way to do both of them together.
Basically I need to send the user to another website temporarily and provide that website with some data entered within the Add-on. I tried doing this with pure javascript, but the window.open() method is not available within the GAS sandbox.
Thanks!
Its not possible in apps script (and not possible in javascript either).
What you need is to open the external site with anchor & needed parameters (post data) in the url itself. Your external url in its javascript must convert the params to a Post (or handle it directly in its server's Get)
Also, if the external page is for doing some authentication with callback (like oauth flow) apps script now support such 'oauth callbacks'

Copy a file from Google Drive to my own server

If you remember, I'm trying to integrate Google Drive within our website, which is built on Elgg. Elgg already has its native file management system.
What we would like to do is to copy a file from Drive to our server, you know, kind of : Send to My Files. The problem is that I don't see any URL in the file metadata indicating where the file is physically stored.
I can see the copy function in Google Drive SDK but I don't think it allows to copy the file on our own server. Unless I've read it wrong.
Can you help me?
Thanks you.
If you are trying to save a file that has content stored in drive (e.g an image, pdf, etc.), the file's metadata should contain a downloadUrl that can be used to retrieve the file's content through an authorized GET request.
For Google Documents (Google Docs, Google Spreadsheets, etc.), the data is stored in a private format that cannot be understand by third party applications. In this case, your app will have to use one of the exposed exportLinks to export the document into a format understood by your application.