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();

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.

2 Likes

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.

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