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       aParameters.put(
 47           "outputPath",
 48           getOutputPath(aFile, aFolderOfPath, theConversionJSON.format)
 49       );
 50     } else
 51     {
 52       REST.pushError(
 53           REST.errors.e400,
 54           "The 'directory' parameter is not set"
 55       );
 56       return;
 57     }
 58   }
 59   if (!!theConversionJSON.destinationColorspace) {
 60     aParameters.put(
 61       "destinationColorspace",
 62       theConversionJSON.destinationColorspace
 63     );
 64   }
 65   if (!!theConversionJSON.scale) {
 66     aParameters.put("scale", theConversionJSON.scale);
 67   }
 68   if (!!theConversionJSON.resolution) {
 69     aParameters.put("resolution", theConversionJSON.resolution);
 70   }
 71   var aNewAssetId = previewManager.addRequestForAsset(
 72     aFile.assetId,
 73     context.getUser(),
 74     aParameters
 75   );
 76   if (aNewAssetId > 0) {
 77     var aConversion = fileManager.getFileObjectById(aNewAssetId);
 78     delete theConversionJSON.id;
 79     delete theConversionJSON.directory;
 80     REST.push(aConversion);
 81   } else {
 82     REST.pushError(
 83       REST.errors.e500,
 84       "Failed to create the conversion: " + JSON.stringify(theConversionJSON)
 85     );
 86   }
 87 }
 88 
 89 /**
 90  * Returns the output path for the conversion.
 91  *
 92  * @param theOrigAsset the original asset
 93  * @param theDestFolder the destination folder
 94  * @param theFormat the output file format
 95  */
 96 var getOutputPath = function (theOrigAsset, theDestFolder, theFormat)
 97 {
 98   var anExtension = kFormatToExtensionMap[theFormat];
 99   if (!anExtension)
100   {
101     anExtension = theFormat;
102   }
103   var aFileNameNoExt = getFileNameNoExtension(theOrigAsset.name);
104   var anOutputPath = theDestFolder.path + aFileNameNoExt + "." + anExtension;
105   var i = 1;
106   while (fileManager.isFile(anOutputPath))
107   {
108     anOutputPath = theDestFolder.path + aFileNameNoExt + "_" + i + "." + anExtension;
109     i++;
110   }
111   return anOutputPath;
112 }
113 
114 /**
115  * Returns the file name without its extension.
116  *
117  * @param theFileName the file name
118  */
119 var getFileNameNoExtension = function (theFileName)
120 {
121   var anIndex = theFileName.lastIndexOf(".");
122   return anIndex > -1 ? theFileName.substring(0, anIndex) : theFileName;
123 };
124 
125 /**
126  * @class Submits assets to the preview system to be converted to another format/resolution/scale/etc...
127  * @description  For every object in "data", this endpoint converts the asset based on the conversion parameters.
128  * @name Conversion
129  * @param data An array of objects.
130  *             Note: directory parameter is required and must point to an existing path.
131  * [
132  *  {
133  *    "id": 123456,
134  *    "directory": "destination/path/",
135  *    "format": "JPEG",
136  *    "destinationColorspace": "RGB",
137  *    "scale": "50%x50%",
138  *    "resolution": "300dpi"
139  *  }, ...
140  * ]
141  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
142  * @param [fields] An array of field id's to collect the values for each asset
143  * @returns [{theConvertedAssetInfo}, ... ]
144  *
145  * @example /wf/restapi/v2/conversion
146  *
147  * Parameters:
148  * data=[
149  *          {"id":201623522,"directory":%20"destination/path/","format":%20"JPEG","destinationColorspace":%20"RGB","scale":%20"50%25x50%25","resolution":%20"300dpi"}
150  *      ]
151  *
152  Response:
153  [
154 	 {
155 	   "id": 201629296,
156 	 }
157  ]
158  * @example /wf/restapi/v2/conversion
159  *
160  * Parameters:
161  * data=[
162  *          {"id":201623522,"directory":%20"destination/path/","format":%20"JPEG","destinationColorspace":%20"RGB","scale":%20"50%25x50%25","resolution":%20"300dpi"},
163  *          {"id":201628936,"directory":%20"destination/path/","format":%20"JPEG","resolution":%20"300dpi"}
164  *      ]
165  * verbose=true
166  *
167  Response:
168  [
169 	 {
170 	   "id": 201629296,
171 	   "name": "dreamstimemaximum_51640045_1.jpg",
172 	   "path": "destination/path/dreamstimemaximum_51640045_1.jpg",
173 	   "height": 1920,
174 	   "width": 2880,
175 	   "bytes": 2527904,
176 	   "lastModified": 1507829025000,
177 	   "mimeType": "image/jpeg",
178 	   "previews": {
179 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323936",
180 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323936",
181 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323936"
182 	   }
183 	 },
184 	 {
185 	   "id": 201629295,
186 	   "name": "NYC.jpg",
187 	   "path": "destination/path/NYC.jpg",
188 	   "height": 752,
189 	   "width": 1280,
190 	   "bytes": 157371,
191 	   "lastModified": 1507827210000,
192 	   "mimeType": "image/jpeg",
193 	   "previews": {
194 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323935",
195 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323935",
196 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323935"
197 	   }
198 	 }
199  ]
200  */
201 function main() {
202   REST.setCallback(convert);
203   return REST.execute("data");
204 }
205 main();
206