1 var REST = library.REST.REST();
  2 
  3 /**
  4  * Maps conversion format to file extension.
  5  */
  6 var kFormatToExtensionMap = {
  7   JPEG: "jpg",
  8   PNG: "png",
  9   PDF: "pdf",
 10   EPS: "eps",
 11   TIFF: "tif",
 12   GIF: "gif",
 13 };
 14 
 15 /**
 16  * Submits the asset to the preview system
 17  * @param theConversionJSON {"id":201623522,"directory":%20"destination/path/","format":%20"JPEG","destinationColorspace":%20"RGB","scale":%20"50%25x50%25","resolution":%20"300dpi"}
 18  */
 19 function convert(theConversionJSON) {
 20   var aFile = fileManager.getFileObjectById(theConversionJSON.id);
 21   if (aFile == null) {
 22     REST.pushError(
 23       REST.errors.e404,
 24       "The file does not exist: " + theConversionJSON.id
 25     );
 26     return;
 27   }
 28   var aParameters = new Parameters();
 29   aParameters.put("quota", 10248000000);
 30   aParameters.put("wait", true);
 31   if (!!theConversionJSON.format) {
 32     aParameters.put("format", theConversionJSON.format);
 33     if (!!theConversionJSON.directory) {
 34       if (
 35         !fileManager.isFolder(REST.getFullPath(theConversionJSON.directory))
 36       ) {
 37         REST.pushError(
 38           REST.errors.e404,
 39           "Directory not found: " + theConversionJSON.directory
 40         );
 41         return;
 42       }
 43       var aFolderOfPath = fileManager.folderNewByPath(
 44         theConversionJSON.directory
 45       );
 46       var aFileName = aFile.name;
 47       var anIndex = aFileName.lastIndexOf(".");
 48       if (anIndex > -1) {
 49         var anExtension = kFormatToExtensionMap[theConversionJSON.format];
 50         if (!anExtension) {
 51           anExtension = theConversionJSON.format;
 52         }
 53         aFileName = aFileName.substring(0, anIndex + 1) + anExtension;
 54         aParameters.put("outputPath", aFolderOfPath.path + aFileName);
 55       }
 56     }
 57   }
 58   if (!!theConversionJSON.destinationColorspace) {
 59     aParameters.put(
 60       "destinationColorspace",
 61       theConversionJSON.destinationColorspace
 62     );
 63   }
 64   if (!!theConversionJSON.scale) {
 65     aParameters.put("scale", theConversionJSON.scale);
 66   }
 67   if (!!theConversionJSON.resolution) {
 68     aParameters.put("resolution", theConversionJSON.resolution);
 69   }
 70   var aNewAssetId = previewManager.addRequestForAsset(
 71     aFile.assetId,
 72     context.getUser(),
 73     aParameters
 74   );
 75   if (aNewAssetId > 0) {
 76     var aConversion = fileManager.getFileObjectById(aNewAssetId);
 77     delete theConversionJSON.id;
 78     delete theConversionJSON.directory;
 79     REST.push(aConversion);
 80   } else {
 81     REST.pushError(
 82       REST.errors.e500,
 83       "Failed to create the conversion: " + JSON.stringify(theConversionJSON)
 84     );
 85   }
 86 }
 87 
 88 /**
 89  * @class Submits assets to the preview system to be converted to another format/resolution/scale/etc...
 90  * @description  For every object in "data", this endpoint converts the asset based on the conversion parameters.
 91  * @name Conversion
 92  * @param data An array of objects.
 93  *             Note: directory parameter is required and must point to an existing path.
 94  * [
 95  *  {
 96  *    "id": 123456,
 97  *    "directory": "destination/path/",
 98  *    "format": "JPEG",
 99  *    "destinationColorspace": "RGB",
100  *    "scale": "50%x50%",
101  *    "resolution": "300dpi"
102  *  }, ...
103  * ]
104  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
105  * @param [fields] An array of field id's to collect the values for each asset
106  * @returns [{theConvertedAssetInfo}, ... ]
107  *
108  * @example /wf/restapi/v2/conversion
109  *
110  * Parameters:
111  * data=[
112  *          {"id":201623522,"directory":%20"destination/path/","format":%20"JPEG","destinationColorspace":%20"RGB","scale":%20"50%25x50%25","resolution":%20"300dpi"}
113  *      ]
114  *
115  Response:
116  [
117 	 {
118 	   "id": 201629296,
119 	 }
120  ]
121  * @example /wf/restapi/v2/conversion
122  *
123  * Parameters:
124  * data=[
125  *          {"id":201623522,"directory":%20"destination/path/","format":%20"JPEG","destinationColorspace":%20"RGB","scale":%20"50%25x50%25","resolution":%20"300dpi"},
126  *          {"id":201628936,"directory":%20"destination/path/","format":%20"JPEG","resolution":%20"300dpi"}
127  *      ]
128  * verbose=true
129  *
130  Response:
131  [
132 	 {
133 	   "id": 201629296,
134 	   "name": "dreamstimemaximum_51640045_1.jpg",
135 	   "path": "destination/path/dreamstimemaximum_51640045_1.jpg",
136 	   "height": 1920,
137 	   "width": 2880,
138 	   "bytes": 2527904,
139 	   "lastModified": 1507829025000,
140 	   "mimeType": "image/jpeg",
141 	   "previews": {
142 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323936",
143 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323936",
144 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323936"
145 	   }
146 	 },
147 	 {
148 	   "id": 201629295,
149 	   "name": "NYC.jpg",
150 	   "path": "destination/path/NYC.jpg",
151 	   "height": 752,
152 	   "width": 1280,
153 	   "bytes": 157371,
154 	   "lastModified": 1507827210000,
155 	   "mimeType": "image/jpeg",
156 	   "previews": {
157 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323935",
158 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323935",
159 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323935"
160 	   }
161 	 }
162  ]
163  */
164 function main() {
165   REST.setCallback(convert);
166   return REST.execute("data");
167 }
168 main();
169