2018-04-24 09:23:59 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const pkgcloud = require('pkgcloud');
|
|
|
|
const streamifier = require('streamifier');
|
2019-10-02 12:07:42 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
2018-04-24 09:23:59 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
provider: 'rackspace-cloudfiles',
|
|
|
|
name: 'Rackspace Cloud',
|
|
|
|
auth: {
|
|
|
|
username: {
|
|
|
|
label: 'Username',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-04-24 09:23:59 +03:00
|
|
|
},
|
|
|
|
apiKey: {
|
|
|
|
label: 'API Key',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-04-24 09:23:59 +03:00
|
|
|
},
|
|
|
|
container: {
|
|
|
|
label: 'Container Name',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-04-24 09:23:59 +03:00
|
|
|
},
|
|
|
|
region: {
|
|
|
|
label: 'Region',
|
|
|
|
type: 'enum',
|
|
|
|
values: [
|
|
|
|
'DFW (Dallas-Fort Worth, TX, US)',
|
|
|
|
'HKG (Hong Kong, China)',
|
|
|
|
'IAD (Blacksburg, VA, US)',
|
|
|
|
'LON (London, England)',
|
2019-09-27 16:26:09 +02:00
|
|
|
'SYD (Sydney, Australia)',
|
|
|
|
],
|
|
|
|
},
|
2018-04-24 09:23:59 +03:00
|
|
|
},
|
2019-09-27 16:26:09 +02:00
|
|
|
init: config => {
|
2018-04-24 09:23:59 +03:00
|
|
|
const options = { container: config.container };
|
|
|
|
const client = pkgcloud.storage.createClient({
|
|
|
|
provider: 'rackspace',
|
|
|
|
username: config.username,
|
|
|
|
apiKey: config.apiKey,
|
2019-09-27 16:26:09 +02:00
|
|
|
region: config.region.replace(/(\s.*\))$/gi, ''),
|
2018-04-24 09:23:59 +03:00
|
|
|
});
|
|
|
|
|
2019-09-27 16:26:09 +02:00
|
|
|
const remoteURL = () =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
return client.getContainer(config.container, (err, res) => {
|
|
|
|
if (err && !res) return reject(err);
|
|
|
|
return resolve(res);
|
|
|
|
});
|
2018-05-04 18:27:39 +02:00
|
|
|
});
|
2018-04-24 09:23:59 +03:00
|
|
|
|
2019-09-27 16:26:09 +02:00
|
|
|
const byteSize = bytes => {
|
2018-04-24 09:23:59 +03:00
|
|
|
if (bytes === 0) return 0;
|
|
|
|
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
|
2019-09-27 16:26:09 +02:00
|
|
|
return i === 0 ? bytes : `${(bytes / 1024 ** i).toFixed(1)}`;
|
2018-04-24 09:23:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2019-09-27 16:26:09 +02:00
|
|
|
upload: file => {
|
|
|
|
const readStream = streamifier.createReadStream(file.buffer);
|
|
|
|
const writeStream = client.upload(
|
|
|
|
Object.assign({}, options, {
|
|
|
|
remote: file.name,
|
|
|
|
contentType: file.mime,
|
|
|
|
})
|
|
|
|
);
|
2018-04-24 09:23:59 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
readStream.pipe(writeStream);
|
2019-09-27 16:26:09 +02:00
|
|
|
writeStream.on('error', error => error && reject(error));
|
|
|
|
writeStream.on('success', result => {
|
|
|
|
remoteURL()
|
|
|
|
.then(data =>
|
|
|
|
resolve(
|
|
|
|
Object.assign(file, {
|
|
|
|
name: result.name,
|
|
|
|
hash: file.hash,
|
|
|
|
ext: file.ext,
|
|
|
|
mime: result.contentType,
|
|
|
|
size: file.size,
|
|
|
|
url: `${data.cdnSslUri}/${result.name}`,
|
|
|
|
provider: 'Rackspace Cloud',
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.catch(err => console.error(err) && reject(err));
|
2018-04-24 09:23:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-09-27 16:26:09 +02:00
|
|
|
delete: file => {
|
2018-04-24 09:23:59 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-09-27 16:26:09 +02:00
|
|
|
client.removeFile(config.container, file.name, error => {
|
2018-04-24 09:23:59 +03:00
|
|
|
if (error) return reject(error);
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2019-09-27 16:26:09 +02:00
|
|
|
},
|
2018-04-24 09:23:59 +03:00
|
|
|
};
|