- md5
- 16c989c9f81f1c424e142fe0d3193647
- sha1
- c5c0607511ac000e1c91060351eb379061c110fa
- sha256
- d34eb7f4f5caabd8a77a4ecb7eb622aae7d6faac74ad875e172b52e110caac29
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
import {notifications} from "./Notifications";
const path = require('path');
/**
* Api class for communication with the server
*/
class Api {
/**
* Store constructor
*/
constructor() {
const options = Joomla.getOptions('com_media', {});
if (options.apiBaseUrl === undefined) {
throw new TypeError('Media api baseUrl is not defined');
}
if (options.csrfToken === undefined) {
throw new TypeError('Media api csrf token is not defined');
}
this._baseUrl = options.apiBaseUrl;
this._csrfToken = Joomla.getOptions('csrf.token');
}
/**
* Get the contents of a directory from the server
* @param {string} dir The directory path
* @param {number} full whether or not the persistent url should be returned
* @param {number} content whether or not the content should be returned
* @returns {Promise}
*/
getContents(dir, full, content) {
// Wrap the ajax call into a real promise
return new Promise((resolve, reject) => {
// Do a check on full
if (["0", "1"].indexOf(full) !== -1) {
throw "Invalid parameter: full";
}
// Do a check on download
if (["0", "1"].indexOf(content) !== -1) {
throw "Invalid parameter: content";
}
let url = this._baseUrl + '&task=api.files&path=' + dir;
if (full) {
url += '&url=' + full;
}
if (content) {
url += '&content=' + content;
}
Joomla.request({
url: url,
method: 'GET',
headers: {'Content-Type': 'application/json'},
onSuccess: (response) => {
resolve(this._normalizeArray(JSON.parse(response).data))
},
onError: (xhr) => {
reject(xhr)
}
});
}).catch(this._handleError);
}
/**
* Create a directory
* @param name
* @param parent
* @returns {Promise.<T>}
*/
createDirectory(name, parent) {
// Wrap the ajax call into a real promise
return new Promise((resolve, reject) => {
const url = this._baseUrl + '&task=api.files&path=' + parent;
const data = {[this._csrfToken]: '1', name: name};
Joomla.request({
url: url,
method: 'POST',
data: JSON.stringify(data),
headers: {'Content-Type': 'application/json'},
onSuccess: (response) => {
notifications.success('COM_MEDIA_CREATE_NEW_FOLDER_SUCCESS');
resolve(this._normalizeItem(JSON.parse(response).data))
},
onError: (xhr) => {
notifications.error('COM_MEDIA_CREATE_NEW_FOLDER_ERROR');
reject(xhr)
}
});
}).catch(this._handleError);
}
/**
* Upload a file
* @param name
* @param parent
* @param content base64 encoded string
* @param override boolean whether or not we should override existing files
* @return {Promise.<T>}
*/
upload(name, parent, content, override) {
// Wrap the ajax call into a real promise
return new Promise((resolve, reject) => {
const url = this._baseUrl + '&task=api.files&path=' + parent;
const data = {
[this._csrfToken]: '1',
name: name,
content: content,
};
// Append override
if (override === true) {
data.override = true;
}
Joomla.request({
url: url,
method: 'POST',
data: JSON.stringify(data),
headers: {'Content-Type': 'application/json'},
onSuccess: (response) => {
notifications.success('COM_MEDIA_UPLOAD_SUCCESS');
resolve(this._normalizeItem(JSON.parse(response).data))
},
onError: (xhr) => {
reject(xhr)
}
});
}).catch(this._handleError);
}
/**
* Rename an item
* @param path
* @param newPath
* @return {Promise.<T>}
*/
rename(path, newPath) {
// Wrap the ajax call into a real promise
return new Promise((resolve, reject) => {
const url = this._baseUrl + '&task=api.files&path=' + path;
const data = {
[this._csrfToken]: '1',
newPath: newPath,
};
Joomla.request({
url: url,
method: 'PUT',
data: JSON.stringify(data),
headers: {'Content-Type': 'application/json'},
onSuccess: (response) => {
notifications.success('COM_MEDIA_RENAME_SUCCESS');
resolve(this._normalizeItem(JSON.parse(response).data))
},
onError: (xhr) => {
notifications.error('COM_MEDIA_RENAME_ERROR');
reject(xhr)
}
});
}).catch(this._handleError);
}
/**
* Delete a file
* @param path
* @return {Promise.<T>}
*/
delete(path) {
// Wrap the ajax call into a real promise
return new Promise((resolve, reject) => {
const url = this._baseUrl + '&task=api.files&path=' + path;
const data = {[this._csrfToken]: '1',};
Joomla.request({
url: url,
method: 'DELETE',
data: JSON.stringify(data),
headers: {'Content-Type': 'application/json'},
onSuccess: () => {
notifications.success('COM_MEDIA_DELETE_SUCCESS');
resolve();
},
onError: (xhr) => {
notifications.error('COM_MEDIA_DELETE_ERROR');
reject(xhr);
}
});
}).catch(this._handleError);
}
/**
* Normalize a single item
* @param item
* @returns {*}
* @private
*/
_normalizeItem(item) {
if (item.type === 'dir') {
item.directories = [];
item.files = [];
}
item.directory = path.dirname(item.path);
if (item.directory.indexOf(':', item.directory.length - 1) !== -1) {
item.directory += '/';
}
return item;
}
/**
* Normalize array data
* @param data
* @returns {{directories, files}}
* @private
*/
_normalizeArray(data) {
const directories = data.filter(item => (item.type === 'dir'))
.map(directory => this._normalizeItem(directory));
const files = data.filter(item => (item.type === 'file'))
.map(file => this._normalizeItem(file));
return {
directories: directories,
files: files,
}
}
/**
* Handle errors
* @param error
* @private
*
* @TODO DN improve error handling
*/
_handleError(error) {
switch (error.status) {
case 409:
// Handled in consumer
break;
case 404:
notifications.error('COM_MEDIA_ERROR_NOT_FOUND');
break;
case 401:
notifications.error('COM_MEDIA_ERROR_NOT_AUTHENTICATED');
break;
case 403:
notifications.error('COM_MEDIA_ERROR_NOT_AUTHORIZED');
break;
case 500:
notifications.error('COM_MEDIA_SERVER_ERROR');
break;
default:
notifications.error('COM_MEDIA_ERROR');
}
throw error;
}
}
export let api = new Api();
The same content is in 6 releases, Joomla 4.0.0-alpha7 to 4.0.0-alpha12. See them all, or every version of this file.