Skip to main content

Url to attachment script : cache issue


Hello,

Is any of you guys familiar with the new native script url to attachement ?

It is looking pretty good, working really smoothly.

But it seems to have a cache issue : the image captured is the same throughout the day.

  1. Do you know how long the cache is preventing a fresh image to be attached ?
  2. Does anyone knows how to edit the script in order to have a fresh new image everytime ?

Thanksfor your help. Hereafter the script code :

let settings = input.config({

title: "Convert URLs to attachments",

description: `This script will download all attachments from a URL into an attachment field. The

source field can be a URL field or any text field containing a URL. Each cell should only contain a single URL.`,

items: [

    input.config.table("table", { label: "Table" }),

    input.config.field("urlField", {

        parentTable: "table",

        label: "URL field",

    }),

    input.config.field("attachmentField", {

        parentTable: "table",

        label: "Attachment field",

        description: "URLs will be downloaded into this field",

    }),

],

});

async function convertURLsToAttachments() {

let { table, urlField, attachmentField } = settings;

if (attachmentField.type !== "multipleAttachments") {

    output.text(

        `${attachmentField.name} is not a attachment field.\nRun the script again when you have an attachment field.`

    );

    return;

}

let skipAlreadySet = await input.buttonsAsync(

    "Skip attachment entries that already have files?",

    [

        { label: "Yes", value: true },

        { label: "No", value: false },

    ]

);

let updates = [];

for (let record of (

    await table.selectRecordsAsync({ fields: [urlField, attachmentField] })

).records) {

    let existingAttachments = record.getCellValue(attachmentField) || [];

    if (skipAlreadySet && existingAttachments.length) continue;

    let urls = record.getCellValueAsString(urlField);

    if (typeof urls !== "string") continue;

    let attachmentsFromUrls = urls.split("\n").map((url) => ({ url }));

    updates.push({

        id: record.id,

        fields: {

            [attachmentField.id]: [

                ...existingAttachments,

                ...attachmentsFromUrls,

            ],

        },

    });

}

for (let i = 0; i < updates.length; i += 50) {

    await table.updateRecordsAsync(updates.slice(i, i + 50));

}

}

await convertURLsToAttachments();

5 replies

  • Inspiring
  • 351 replies
  • June 21, 2021

Change that line to:

let attachmentsFromUrls = urls.split("\n").map((url) => ({ url: url + "?cacheBuster=" + Date.now() }));

That will put a dynamic parameter that is based on current time to make sure its always unique from previous requests.


  • New Participant
  • 4 replies
  • July 28, 2021

Adding the cacheBuster url parameter fixed my problem but now all my files names include it and when I download a file my computer doesn’t know what do with it. :frowning: Any ideas how to fix that?


chrowe wrote:

Adding the cacheBuster url parameter fixed my problem but now all my files names include it and when I download a file my computer doesn’t know what do with it. :frowning: Any ideas how to fix that?


Are you sure they aren’t opening at all? Or are they only opening once?

Either way, can’t you just rename them?


kuovonne
Forum|alt.badge.img+17
  • Brainy
  • 5992 replies
  • July 29, 2021
chrowe wrote:

Adding the cacheBuster url parameter fixed my problem but now all my files names include it and when I download a file my computer doesn’t know what do with it. :frowning: Any ideas how to fix that?


When submitting the request to upload the file, you can specify the original file name along with the url. See the documentation for more info.


  • New Participant
  • 4 replies
  • July 30, 2021

Thanks, for some reason I couldn’t find that section of the docs. Worked like a charm!


Reply