Help

Re: Url to attachment script : cache issue

1253 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Gregoire_Ch
4 - Data Explorer
4 - Data Explorer

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 5
openside
10 - Mercury
10 - Mercury

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.

chrowe
5 - Automation Enthusiast
5 - Automation Enthusiast

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?

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.

chrowe
5 - Automation Enthusiast
5 - Automation Enthusiast

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