Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2375-demo-theme-development' of github.com:okfn/ckan in…
…to 2375-demo-theme-development
- Loading branch information
Showing
36 changed files
with
4,133 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
(function (ckan, jQuery) { | ||
|
||
function Client() {} | ||
|
||
jQuery.extend(Client.prototype, { | ||
|
||
/* Requests config options for a file upload. | ||
* | ||
* See: http://docs.ckan.org/en/latest/filestore.html | ||
* | ||
* key - A unique id/filename for the resource. | ||
* success - A callback to be called on successful response. | ||
* error - A callback to be called when the request fails. | ||
* | ||
* Examples | ||
* | ||
* client.getStorageAuth('myfile.jpg', function (data) { | ||
* data.fields; | ||
* }); | ||
* | ||
* client.getStorageAuth('myfile.jpg') | ||
* .done(function (data) { | ||
* data.fields; | ||
* }) | ||
* .error(function () { | ||
* showError('Something Went Wrong'); | ||
* }); | ||
* | ||
* Returns a jqXHR promise. | ||
*/ | ||
getStorageAuth: function (key, success, error) { | ||
if (!key) { | ||
throw new Error('Client#getStorageAuth() must be called with a key'); | ||
} | ||
|
||
return jQuery.ajax({ | ||
url: '/api/storage/auth/form/' + key, | ||
success: success, | ||
error: error | ||
}); | ||
}, | ||
|
||
/* Requests metadata for a file upload. | ||
* | ||
* See: http://docs.ckan.org/en/latest/filestore.html | ||
* | ||
* key - A unique id/filename for the resource. | ||
* success - A callback to be called on successful response. | ||
* error - A callback to be called when the request fails. | ||
* | ||
* Examples | ||
* | ||
* client.getStorageMetadata('myfile.jpg', function (data) { | ||
* data._format; | ||
* }); | ||
* | ||
* Returns a jqXHR promise. | ||
*/ | ||
getStorageMetadata: function (key, success, error) { | ||
if (!key) { | ||
throw new Error('Client#getStorageMetadata() must be called with a key'); | ||
} | ||
|
||
return jQuery.ajax({ | ||
url: '/api/storage/metadata/' + key, | ||
success: success, | ||
error: error | ||
}); | ||
}, | ||
|
||
/* Converts the data returned from the storage metadata into keys that | ||
* can be used with a dataset. | ||
* | ||
* key - The key for the stored file. | ||
* meta - The metadata object. | ||
* | ||
* Examples | ||
* | ||
* client.getStorageMetadata('myfile.jpg', function (data) { | ||
* var dataset = client.convertStorageMetadataToResource(data); | ||
* }); | ||
* | ||
* Returns an object of dataset keys. | ||
*/ | ||
convertStorageMetadataToResource: function (meta) { | ||
var modified = new Date(meta._last_modified); | ||
var modifiedISO = jQuery.date.toISOString(modified); | ||
var filename = meta['filename-original'] || meta.key; | ||
var format = meta._format || filename.split('.').pop(); | ||
|
||
return { | ||
url: meta._location, | ||
key: meta.key, /* Not strictly Resource data but may be useful */ | ||
name: filename, | ||
size: meta._content_length, | ||
last_modified: modifiedISO, | ||
format: format, | ||
mimetype: format, | ||
resource_type: 'file.upload', // Is this standard? | ||
owner: meta['uploaded-by'], | ||
hash: meta._checksum, | ||
cache_url: meta._location, | ||
cache_url_updated: modifiedISO | ||
}; | ||
} | ||
}); | ||
|
||
ckan.sandbox.setup(function (instance) { | ||
instance.client = new Client(); | ||
}); | ||
|
||
ckan.Client = Client; | ||
|
||
})(this.ckan, this.jQuery); |
Oops, something went wrong.