Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnajdek/b2392ee558314aa0d559475b3ff3cf71 to your computer and use it in GitHub Desktop.
Save tnajdek/b2392ee558314aa0d559475b3ff3cf71 to your computer and use it in GitHub Desktop.
const api = require('zotero-api-client'); // >= 0.36.0
const fs = require('fs');
const SparkMD5 = require('spark-md5');
const filedata1 = fs.readFileSync('picture-v1.jpg');
const filedata2 = fs.readFileSync('picture-v2.jpg');
const APIKEY = '';
const USERID = '';
const ITEM_KEY = '';
(async () => {
try {
// get attachment item template
const template = (await api()
.template('attachment')
.get({ linkMode: 'imported_file' })).getData();
console.log('template', template);
// create attachment item
const item = {
...template,
title: 'attached picture will override',
parentItem: ITEM_KEY,
filename: 'picture.jpg',
contentType: 'image/jpeg',
};
const attachmentItem = (await api(APIKEY)
.library(USERID)
.items()
.post([item])).getEntityByIndex(0);
console.log('attachmentItem', attachmentItem);
// upload first version of the file
const response1 = await api(APIKEY)
.library(USERID)
.items(attachmentItem.key)
.attachment('picture.jpg', filedata1.buffer)
.post();
console.log(response1);
// calculate checksum of the first attachment file
const md5sum = SparkMD5.ArrayBuffer.hash(filedata1.buffer);
// upload second version of the file overriding previous one
const response2 = await api(APIKEY)
.library(USERID)
.items(attachmentItem.key)
.attachment('picture.jpg', filedata2.buffer, null, md5sum)
.post();
console.log(response2);
} catch(e) {
console.warn(e, e.reason, e.message, e.options);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment