|
| 1 | +/** |
| 2 | + * Asset instances |
| 3 | + * @namespace Asset |
| 4 | + */ |
1 | 5 | import {cloneDeep} from 'lodash/lang'
|
2 | 6 | import freezeSys from 'contentful-sdk-core/freeze-sys'
|
3 | 7 | import enhanceWithMethods from '../enhance-with-methods'
|
4 | 8 | import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
|
| 9 | +import errorHandler from '../error-handler' |
| 10 | +import { |
| 11 | + createUpdateEntity, |
| 12 | + createDeleteEntity, |
| 13 | + createPublishEntity, |
| 14 | + createUnpublishEntity, |
| 15 | + createArchiveEntity, |
| 16 | + createUnarchiveEntity |
| 17 | +} from '../instance-actions' |
5 | 18 |
|
6 |
| -// Methods to add: |
7 |
| -// - update |
8 |
| -// - publish |
9 |
| -// - unpublish |
10 |
| -// - delete |
11 |
| -// - archive |
12 |
| -// - unarchive |
13 |
| -// - process (should check on it's own if the asset is processed and timeout, maybe allow timeout as parameter) |
| 19 | +const ASSET_PROCESSING_CHECK_WAIT = 500 |
| 20 | +const ASSET_PROCESSING_CHECK_RETRIES = 5 |
14 | 21 |
|
15 | 22 | /**
|
16 | 23 | * @memberof Entities
|
17 | 24 | * @typedef Asset
|
18 |
| - * @prop {Entities.Sys} sys - Standard system metadata with additional entry specific properties |
| 25 | + * @prop {Entities.Sys} sys - Standard system metadata with additional asset specific properties |
19 | 26 | * @prop {string=} sys.locale - If present, indicates the locale which this asset uses
|
20 | 27 | * @prop {Object} fields - Object with content for each field
|
21 | 28 | * @prop {string} fields.title - Title for this asset
|
22 | 29 | * @prop {string} fields.description - Description for this asset
|
23 | 30 | * @prop {Object} fields.file - File object for this asset
|
24 | 31 | * @prop {Object} fields.file.fileName - Name for the file
|
25 | 32 | * @prop {string} fields.file.contentType - Mime type for the file
|
26 |
| - * @prop {string} fields.file.url - Url where the file is available at. |
| 33 | + * @prop {string=} fields.file.upload - Url where the file is available to be downloaded from, into the Contentful asset system. After the asset is processed this field is gone. |
| 34 | + * @prop {string=} fields.file.url - Url where the file is available at the Contentful media asset system. This field won't be available until the asset is processed. |
27 | 35 | * @prop {Object} fields.file.details - Details for the file, depending on file type (example: image size in bytes, etc)
|
28 | 36 | * @prop {function(): Object} toPlainObject() - Returns this Asset as a plain JS object
|
29 | 37 | */
|
30 | 38 |
|
31 | 39 | function createAssetApi (http) {
|
32 |
| - return {} |
| 40 | + function checkIfAssetHasUrl (resolve, reject, id, locale, checkCount = 0) { |
| 41 | + http.get('assets/' + id) |
| 42 | + .then((response) => wrapAsset(http, response.data), errorHandler) |
| 43 | + .then((asset) => { |
| 44 | + if (asset.fields.file[locale].url) { |
| 45 | + resolve(asset) |
| 46 | + } else if (checkCount === ASSET_PROCESSING_CHECK_RETRIES) { |
| 47 | + const error = new Error() |
| 48 | + error.name = 'AssetProcessingTimeout' |
| 49 | + error.message = 'Asset is taking longer then expected to process.' |
| 50 | + reject(error) |
| 51 | + } else { |
| 52 | + checkCount++ |
| 53 | + setTimeout( |
| 54 | + () => checkIfAssetHasUrl(resolve, reject, id, locale, checkCount), |
| 55 | + ASSET_PROCESSING_CHECK_WAIT |
| 56 | + ) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + function processForLocale (locale) { |
| 62 | + const assetId = this.sys.id |
| 63 | + return http.put('assets/' + this.sys.id + '/files/' + locale + '/process', null, { |
| 64 | + headers: { |
| 65 | + 'X-Contentful-Version': this.sys.version |
| 66 | + } |
| 67 | + }) |
| 68 | + .then(() => { |
| 69 | + return new Promise( |
| 70 | + (resolve, reject) => checkIfAssetHasUrl(resolve, reject, assetId, locale) |
| 71 | + ) |
| 72 | + }, errorHandler) |
| 73 | + } |
| 74 | + |
| 75 | + function processForAllLocales () { |
| 76 | + const self = this |
| 77 | + const locales = Object.keys(this.fields.file) |
| 78 | + return Promise.all(locales.map((locale) => processForLocale.call(self, locale))) |
| 79 | + .then((assets) => assets[0]) |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + /** |
| 84 | + * Sends an update to the server with any changes made to the object's properties |
| 85 | + * @memberof Asset |
| 86 | + * @func update |
| 87 | + * @return {Promise<Asset>} Object returned from the server with updated changes. |
| 88 | + * @example |
| 89 | + * contentType.fields.name['en-US'] = 'Blog Post' |
| 90 | + * asset.update() |
| 91 | + * .then(asset => console.log(asset.fields.name['en-US'])) |
| 92 | + */ |
| 93 | + update: createUpdateEntity({ |
| 94 | + http: http, |
| 95 | + entityPath: 'assets', |
| 96 | + wrapperMethod: wrapAsset |
| 97 | + }), |
| 98 | + |
| 99 | + /** |
| 100 | + * Deletes this object on the server. |
| 101 | + * @memberof Asset |
| 102 | + * @func delete |
| 103 | + * @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled. |
| 104 | + * @example |
| 105 | + * asset.delete() |
| 106 | + * .catch(err => console.log(err)) |
| 107 | + */ |
| 108 | + delete: createDeleteEntity({ |
| 109 | + http: http, |
| 110 | + entityPath: 'assets' |
| 111 | + }), |
| 112 | + |
| 113 | + /** |
| 114 | + * Publishes the object |
| 115 | + * @memberof Asset |
| 116 | + * @func publish |
| 117 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 118 | + * @example |
| 119 | + * asset.publish() |
| 120 | + * .then(asset => console.log(asset.sys.publishedVersion)) |
| 121 | + */ |
| 122 | + publish: createPublishEntity({ |
| 123 | + http: http, |
| 124 | + entityPath: 'assets', |
| 125 | + wrapperMethod: wrapAsset |
| 126 | + }), |
| 127 | + |
| 128 | + /** |
| 129 | + * Unpublishes the object |
| 130 | + * @memberof Asset |
| 131 | + * @func unpublish |
| 132 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 133 | + * @example |
| 134 | + * asset.unpublish() |
| 135 | + * .then(asset => console.log(asset.sys)) |
| 136 | + */ |
| 137 | + unpublish: createUnpublishEntity({ |
| 138 | + http: http, |
| 139 | + entityPath: 'assets', |
| 140 | + wrapperMethod: wrapAsset |
| 141 | + }), |
| 142 | + |
| 143 | + /** |
| 144 | + * Archives the object |
| 145 | + * @memberof Asset |
| 146 | + * @func archive |
| 147 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 148 | + * @example |
| 149 | + * asset.archive() |
| 150 | + * .then(asset => console.log(asset.sys.archivedVersion)) |
| 151 | + */ |
| 152 | + archive: createArchiveEntity({ |
| 153 | + http: http, |
| 154 | + entityPath: 'assets', |
| 155 | + wrapperMethod: wrapAsset |
| 156 | + }), |
| 157 | + |
| 158 | + /** |
| 159 | + * Unarchives the object |
| 160 | + * @memberof Asset |
| 161 | + * @func unarchive |
| 162 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 163 | + * @example |
| 164 | + * asset.unarchive() |
| 165 | + * .then(asset => console.log(asset.sys)) |
| 166 | + */ |
| 167 | + unarchive: createUnarchiveEntity({ |
| 168 | + http: http, |
| 169 | + entityPath: 'assets', |
| 170 | + wrapperMethod: wrapAsset |
| 171 | + }), |
| 172 | + |
| 173 | + /** |
| 174 | + * Triggers asset processing after an upload, for the file uploaded to a specific locale. |
| 175 | + * @memberof Asset |
| 176 | + * @func processForLocale |
| 177 | + * @param {string} locale - Locale which processing should be triggered for |
| 178 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 179 | + * @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged. |
| 180 | + * @example |
| 181 | + * asset.processForLocale('en-US') |
| 182 | + * .then(asset => console.log(asset.fields.file['en-US'].url)) |
| 183 | + */ |
| 184 | + processForLocale: processForLocale, |
| 185 | + |
| 186 | + /** |
| 187 | + * Triggers asset processing after an upload, for the files uploaded to all locales of an asset. |
| 188 | + * @memberof Asset |
| 189 | + * @func processForAllLocales |
| 190 | + * @return {Promise<Asset>} Object returned from the server with updated metadata. |
| 191 | + * @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged. |
| 192 | + * @example |
| 193 | + * asset.processForAllLocales() |
| 194 | + * .then(asset => console.log(asset.fields.file['en-US'].url, asset.fields.file['de-DE'].url)) |
| 195 | + */ |
| 196 | + processForAllLocales: processForAllLocales |
| 197 | + } |
33 | 198 | }
|
34 | 199 |
|
35 | 200 | /**
|
|
0 commit comments