1 var REST = library.REST.REST();
  2 
  3 function checkIn() {
  4   var aToVersion = fileManager.getFileObjectById(context.getParameter("id"));
  5   var aMessage = context.getParameter("message", "");
  6   var aReplaceName = context.getParameter("replaceName");
  7   aReplaceName =
  8     aReplaceName == null || aReplaceName == undefined ? false : aReplaceName;
  9   var aReplaceMetadata = context.getParameter("replaceMetadata");
 10   aReplaceMetadata =
 11     aReplaceMetadata == null || aReplaceMetadata == undefined
 12       ? false
 13       : aReplaceMetadata;
 14 
 15   try {
 16     var anUploadedFile = context.getUploadedFile();
 17     if (anUploadedFile == null) {
 18       REST.pushError(
 19         REST.errors.e400,
 20         "A file was not attached to the form-data of the Body of the POST"
 21       );
 22       return;
 23     }
 24     if (!aToVersion) {
 25       REST.pushError(
 26         REST.errors.e400,
 27         "the 'id' parameter needs to be set to an asset id to version"
 28       );
 29       return;
 30     }
 31     if (searchManager.filterByACL([aToVersion.assetId]).length == 0) {
 32       REST.pushError(
 33         REST.errors.e404,
 34         "The file does not exist: " + aToVersion.assetId
 35       );
 36       return;
 37     }
 38     versionManager.createNewVersion(
 39       aToVersion.assetId,
 40       anUploadedFile,
 41       aMessage,
 42       !!aReplaceName,
 43       !!aReplaceMetadata
 44     );
 45     REST.push(fileManager.getFileObjectById(aToVersion.assetId));
 46   } catch (err) {
 47     REST.pushError(
 48       REST.errors.e500,
 49       "Failed to upload a new version: " + aToVersion,
 50       err
 51     );
 52   }
 53 }
 54 
 55 /**
 56  * @name CheckIn
 57  * @class Upload a new version of an asset into MediaBeacon
 58  * @description Upload a new version of an asset into MediaBeacon
 59  * @param id  the id of the asset to upload a new version for
 60  * @param message the message for the new version
 61  * @param file the file needs to be added to the body of the POST in the form data with the name "file"
 62  * @param [replaceName=false] replace the name of the asset with the attached files name?
 63  * @param [replaceMetadata=false] replace the metadata with the metadata of the attached file?
 64  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 65  * @param [fields] An array of field id's to collect the values for each asset
 66  * @returns [{assetInfo}, ... ]
 67  *
 68  * @example /wf/restapi/v2/checkIn
 69  *
 70  * Parameters:
 71  * id=201629401
 72  * file={{FILE DATA IN POST BODY}}
 73  * verbose=true
 74  *
 75  * Response:
 76  [
 77     {
 78 		 "id": 201629401,
 79 		 "name": "heic0006a_orig.tif",
 80 		 "path": "upload/here/heic0006a_orig.tif",
 81 		 "height": 746,
 82 		 "width": 1500,
 83 		 "bytes": 1459026,
 84 		 "lastModified": 1508187662000,
 85 		 "mimeType": "image/tiff",
 86 		 "previews": {
 87 			 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239343031",
 88 			 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239343031",
 89 			 "downloadUrl": "../servlet/dload?id=pe_323031363239343031"
 90 		 }
 91 	 }
 92  ]
 93  */
 94 function main() {
 95   if (
 96     (!context.getUser().isGlobalAdmin() &&
 97       !context.getUser().isPermissionEnabled("checkOut")) ||
 98     (context.getUser().isGlobalAdmin() &&
 99       !context.getUser().isPermissionEnabled("checkOutAdmin"))
100   ) {
101     REST.pushError(
102       REST.errors.e403,
103       "checkOut or checkOutAdmin permission is not enabled"
104     );
105     return REST.execute();
106   }
107   if (searchManager.filterByACL([context.getParameter("id")]).length == 0) {
108     REST.pushError(
109       REST.errors.e404,
110       "The file does not exist: " + context.getParameter("id")
111     );
112     REST.execute();
113   }
114   REST.setCallback(checkIn);
115   return REST.execute();
116 }
117 main();
118