Jun 21, 2021 04:58 AM
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.
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();
Jun 21, 2021 09:09 AM
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.
Jul 28, 2021 08:39 AM
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?
Jul 29, 2021 09:30 AM
Are you sure they aren’t opening at all? Or are they only opening once?
Either way, can’t you just rename them?
Jul 29, 2021 12:28 PM
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.
Jul 30, 2021 05:31 AM
Thanks, for some reason I couldn’t find that section of the docs. Worked like a charm!