Skip to content

Commit 001fb41

Browse files
authored
Merge pull request #101 from imagekit-developer/IK-1487
added isPublished and publish object during upload and updateFileDetails respectively
2 parents 9fe6eb0 + bb41a5b commit 001fb41

File tree

8 files changed

+82
-13
lines changed

8 files changed

+82
-13
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ imagekit.upload({
351351
}
352352
]
353353
},
354-
checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
354+
checks: {`"file.size" < "1mb"`}, // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
355+
isPublished: true
355356
}, function(error, result) {
356357
if(error) console.log(error);
357358
else console.log(result);
@@ -502,6 +503,8 @@ imagekit.getFileVersionDetails({
502503

503504
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details). The first argument to the `updateFileDetails` method is the file ID, and the second argument is an object with the parameters to be updated.
504505

506+
Note: If `publish` is included in the update options, no other parameters are allowed. If any are present, an error will be returned: `Your request cannot contain any other parameters when publish is present`.
507+
505508
```js
506509
// Using Callback Function
507510

@@ -524,15 +527,10 @@ imagekit.updateFileDetails("file_id", {
524527
// Using Promises
525528

526529
imagekit.updateFileDetails("file_id", {
527-
tags : ['image_tag'],
528-
customCoordinates : "10,10,100,100",
529-
extensions: [
530-
{
531-
name: "google-auto-tagging",
532-
maxTags: 5,
533-
minConfidence: 95
534-
}
535-
]
530+
publish: {
531+
isPublished: true,
532+
includeFileVersions: true
533+
}
536534
}).then(response => {
537535
console.log(response);
538536
}).catch(error => {

libs/interfaces/FileDetails.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export interface FileDetailsOptions {
7272
* A key-value data to be associated with the asset. To unset a key, send null value for that key. Before setting any custom metadata on an asset you have to create the field using custom metadata fields API.
7373
*/
7474
customMetadata?: CMValues;
75+
/**
76+
* Configure the publication status of a file and its versions.
77+
*/
78+
publish?: {
79+
isPublished: boolean;
80+
includeFileVersions?: boolean;
81+
};
7582
}
7683

7784
/**

libs/interfaces/UploadOptions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,10 @@ export interface UploadOptions {
112112
* Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
113113
*/
114114
checks?: string
115+
/**
116+
* Optional. Determines whether the file should be uploaded as published.
117+
* If set to false, the file will be marked as unpublished, restricting access to the file through the media library only.
118+
* Files in draft or unpublished states can only be publicly accessed after they are published.
119+
*/
120+
isPublished?: boolean
115121
}

libs/interfaces/UploadResponse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ export interface UploadResponse {
7676
* A key-value data associated with the asset. Before setting any custom metadata on an asset, you have to create the field using custom metadata fields API.
7777
*/
7878
customMetadata?: CMValues;
79+
/**
80+
* Is the file published or in draft state. It can be either true or false.
81+
*/
82+
isPublished?: boolean
7983
}

libs/manage/file.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,22 @@ const updateDetails = function (
202202
respond(true, errorMessages.UPDATE_DATA_MISSING, callback);
203203
return;
204204
}
205-
var data = {
205+
206+
var data = {};
207+
data = {
206208
tags: updateData.tags,
207209
customCoordinates: updateData.customCoordinates,
208210
extensions: updateData.extensions,
209211
webhookUrl: updateData.webhookUrl,
210-
customMetadata: updateData.customMetadata
212+
customMetadata: updateData.customMetadata,
211213
};
212214

215+
if (updateData.publish)
216+
data = {
217+
...data,
218+
publish: updateData.publish,
219+
};
220+
213221
var requestOptions = {
214222
url: "https://api.imagekit.io/v1/files/" + fileId + "/details",
215223
method: "PATCH",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "5.1.0",
3+
"version": "5.2.0",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

tests/mediaLibrary.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,28 @@ describe("Media library APIs", function () {
780780
imagekit.updateFileDetails(fileId, updateData);
781781
});
782782

783+
784+
it('Update publish status', function (done) {
785+
var fileId = "23902390239203923";
786+
787+
var updateData = {
788+
publish: {
789+
isPublished: false,
790+
},
791+
};
792+
793+
const scope = nock('https://api.imagekit.io')
794+
.patch(`/v1/files/${fileId}/details`)
795+
.basicAuth({ user: initializationParams.privateKey, pass: '' })
796+
.reply(200, function (uri, requestBody) {
797+
expect(this.req.path).equal(`/v1/files/${fileId}/details`);
798+
expect(requestBody).to.deep.equal(updateData);
799+
done()
800+
})
801+
802+
imagekit.updateFileDetails(fileId, updateData);
803+
});
804+
783805
it('Update file details invalid updateData', function (done) {
784806
var fileId = "23902390239203923";
785807

tests/upload.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,28 @@ describe("File upload", function () {
572572

573573
imagekit.upload(fileOptions, callback);
574574
});
575+
576+
it("With isPublished option", function (done) {
577+
const fileOptions = {
578+
fileName: "test_file_name",
579+
file: "test_file_content",
580+
isPublished: false
581+
};
582+
583+
var callback = sinon.spy();
584+
585+
const scope = nock("https://upload.imagekit.io/api")
586+
.post("/v1/files/upload")
587+
.basicAuth({ user: initializationParams.privateKey, pass: "" })
588+
.reply(200, function (uri, requestBody) {
589+
expect(this.req.headers["content-type"]).include("multipart/form-data; boundary=---------------------");
590+
var boundary = this.req.headers["content-type"].replace("multipart/form-data; boundary=", "");
591+
checkFormData({ requestBody, boundary, fieldName: "fileName", fieldValue: fileOptions.fileName });
592+
checkFormData({ requestBody, boundary, fieldName: "file", fieldValue: fileOptions.file });
593+
checkFormData({ requestBody, boundary, fieldName: "isPublished", fieldValue: fileOptions.isPublished });
594+
done();
595+
});
596+
597+
imagekit.upload(fileOptions, callback);
598+
});
575599
});

0 commit comments

Comments
 (0)