Help

Attachment resizing

1247 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Nikola_Radakovi
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi guys!

Can anyone help with modifying a script bellow so it forks with automations when a new record is created?

Currently, the script works when a button is pressed. The script extension is activated and attachments are resized.

The script :

let tinyPngApiKey = 'tinyPngApiKey;
let airtableAttachmentSource = 'Source';
let airtableAttachmentDestination = 'Destination;
let airtableColumnToLog = 'Compress';

let table = base.getTable('baseName');
let record = await input.recordAsync('Select a record to compress attachments:', table);

let recordAttachmentUrls = record.getCellValue(airtableAttachmentSource);

console.log('Compressing attachments for the selected record');

let compressedAttachmentUrls = [];

if (Array.isArray(recordAttachmentUrls)) {
  for (let i = 0; i < recordAttachmentUrls.length; i++) {
    let recordAttachmentUrl = recordAttachmentUrls[i]['url'];

    let request = await remoteFetchAsync('https://api.tinify.com/shrink', {
      body: JSON.stringify({ 'source': { 'url': recordAttachmentUrl } }),
      headers: {
        Authorization: 'Basic ' + btoa('api:' + tinyPngApiKey),
        'Content-Type': 'application/json'
      },
      method: 'POST'
    });

    const json = await request.json();

    // Checks that the API didn't fail.
    if (request.status == 201) {
      let percentReduced = Math.round((1 - json.output.ratio) * 100);
      let kbReduced = (json.input.size - json.output.size) / 1024;

      console.log('Panda just saved you ' + percentReduced + '% (' + Math.round(kbReduced) + 'KB).');

      compressedAttachmentUrls.push({ url: json['output']['url'] });
    }
  }
}

await table.updateRecordAsync(record.id, {
  [airtableAttachmentDestination]: compressedAttachmentUrls,
});
4 Replies 4
Alexey_Gusev
12 - Earth
12 - Earth

Hi,
At first, remoteFetchAsync won't work in automation, try to use 'fetch'. in button script at first.

then, hardcode upper 4 variables on the left side of automation code editor, put names and values 
and add fifth, 'id' with '+' add trigger record ID in value

then

 

let {tinyPngApiKey, airtableAttachmentSource, airtableAttachmentDestination, airtableColumnToLog, id}=input.config()

 

I would suggest shorter names, like fileSource, fileDestination etc..., but that's up to you

 

let table = base.getTable('baseName'); 
let record=await table.SelectRecordAsync(id)

 

note: ..RecordAsync.. , not RecordsAsync, you need just one record

other part of script should work as is
if 'fetch' fails in button script, there is a chance it might work in automation.

p.s. automation run limit is 30s. with fetching each file in a loop, I think, it can't process more than 5(?) files in a cell. it depends on several factors and you can evaluate the time running from button. Automation usually work slower than script.

Nikola_Radakovi
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi Alexey,

I will give it a try and get back with results. Thanks for reaching out 👍

Alexey,

I've managed to get script working but, like you said, when exceeding automation run limit of 30s automation fails.

Is there a way around it?

It depends on different 'how much'-es.
how much files usually in a cell, how much time it takes for 1 file, 3 or 5 files
if you have enough automation runs, you could add image process field (array of URLs in text form, comma separated),, second automation should read cell, split to array, pop last/first value, write the rest back to field, process taken element.
with 'write blank, then write value' you can use process field with 'matches conditions' (= non empty) trigger
so, it will shrink one by one.

Also search tinify API for batch file shrinks - if present, it could be other option. I did a quick look and find nothing