2018-04-29 14:37:51 +02:00
|
|
|
import OS from '../../mios';
|
2018-02-20 18:53:34 +01:00
|
|
|
import { apiUrl } from '../../config';
|
|
|
|
import CropWindow from '../views/components/crop-window.vue';
|
|
|
|
import ProgressDialog from '../views/components/progress-dialog.vue';
|
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
export default (os: OS) => {
|
2018-02-20 18:53:34 +01:00
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
const cropImage = file => new Promise((resolve, reject) => {
|
2018-08-27 21:03:48 +02:00
|
|
|
|
2018-08-28 10:44:49 +02:00
|
|
|
const regex = RegExp('\.(jpg|jpeg|png|gif|webp|bmp|tiff)$');
|
|
|
|
if (!regex.test(file.name) ) {
|
2018-08-27 21:03:48 +02:00
|
|
|
os.apis.dialog({
|
|
|
|
title: '%fa:info-circle% %i18n:desktop.invalid-filetype%',
|
|
|
|
text: null,
|
|
|
|
actions: [{
|
|
|
|
text: '%i18n:common.got-it%'
|
|
|
|
}]
|
|
|
|
});
|
2018-08-30 22:04:20 +02:00
|
|
|
return reject('invalid-filetype');
|
2018-08-27 21:03:48 +02:00
|
|
|
}
|
2018-08-28 10:44:49 +02:00
|
|
|
|
2018-05-27 06:49:09 +02:00
|
|
|
const w = os.new(CropWindow, {
|
|
|
|
image: file,
|
2018-08-06 20:20:26 +02:00
|
|
|
title: '%i18n:desktop.banner-crop-title%',
|
2018-05-27 06:49:09 +02:00
|
|
|
aspectRatio: 16 / 9
|
|
|
|
});
|
2018-02-20 18:53:34 +01:00
|
|
|
|
|
|
|
w.$once('cropped', blob => {
|
|
|
|
const data = new FormData();
|
2018-05-27 06:49:09 +02:00
|
|
|
data.append('i', os.store.state.i.token);
|
2018-02-20 18:53:34 +01:00
|
|
|
data.append('file', blob, file.name + '.cropped.png');
|
|
|
|
|
|
|
|
os.api('drive/folders/find', {
|
2018-08-06 20:20:26 +02:00
|
|
|
name: '%i18n:desktop.banner%'
|
2018-02-20 18:53:34 +01:00
|
|
|
}).then(bannerFolder => {
|
|
|
|
if (bannerFolder.length === 0) {
|
|
|
|
os.api('drive/folders/create', {
|
2018-08-06 20:20:26 +02:00
|
|
|
name: '%i18n:desktop.banner%'
|
2018-02-20 18:53:34 +01:00
|
|
|
}).then(iconFolder => {
|
2018-04-08 17:33:56 +02:00
|
|
|
resolve(upload(data, iconFolder));
|
2018-02-20 18:53:34 +01:00
|
|
|
});
|
|
|
|
} else {
|
2018-04-08 17:33:56 +02:00
|
|
|
resolve(upload(data, bannerFolder[0]));
|
2018-02-20 18:53:34 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
w.$once('skipped', () => {
|
2018-04-08 17:33:56 +02:00
|
|
|
resolve(file);
|
2018-02-20 18:53:34 +01:00
|
|
|
});
|
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
w.$once('cancelled', reject);
|
|
|
|
|
2018-02-20 18:53:34 +01:00
|
|
|
document.body.appendChild(w.$el);
|
2018-04-08 17:33:56 +02:00
|
|
|
});
|
2018-02-20 18:53:34 +01:00
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
const upload = (data, folder) => new Promise((resolve, reject) => {
|
2018-05-27 06:49:09 +02:00
|
|
|
const dialog = os.new(ProgressDialog, {
|
2018-08-06 20:20:26 +02:00
|
|
|
title: '%i18n:desktop.uploading-banner%'
|
2018-05-27 06:49:09 +02:00
|
|
|
});
|
2018-02-20 18:53:34 +01:00
|
|
|
document.body.appendChild(dialog.$el);
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (folder) data.append('folderId', folder.id);
|
2018-02-20 18:53:34 +01:00
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
|
|
|
xhr.onload = e => {
|
|
|
|
const file = JSON.parse((e.target as any).response);
|
|
|
|
(dialog as any).close();
|
2018-04-08 17:33:56 +02:00
|
|
|
resolve(file);
|
2018-02-20 18:53:34 +01:00
|
|
|
};
|
2018-04-08 17:33:56 +02:00
|
|
|
xhr.onerror = reject;
|
2018-02-20 18:53:34 +01:00
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
2018-02-20 21:55:19 +01:00
|
|
|
if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
|
2018-02-20 18:53:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(data);
|
2018-04-08 17:33:56 +02:00
|
|
|
});
|
2018-02-20 18:53:34 +01:00
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
const setBanner = file => {
|
|
|
|
return os.api('i/update', {
|
2018-03-29 07:48:47 +02:00
|
|
|
bannerId: file.id
|
2018-02-20 18:53:34 +01:00
|
|
|
}).then(i => {
|
2018-05-27 06:49:09 +02:00
|
|
|
os.store.commit('updateIKeyValue', {
|
|
|
|
key: 'bannerId',
|
|
|
|
value: i.bannerId
|
|
|
|
});
|
|
|
|
os.store.commit('updateIKeyValue', {
|
|
|
|
key: 'bannerUrl',
|
|
|
|
value: i.bannerUrl
|
|
|
|
});
|
2018-02-20 21:55:19 +01:00
|
|
|
|
2018-02-20 18:53:34 +01:00
|
|
|
os.apis.dialog({
|
2018-08-06 20:20:26 +02:00
|
|
|
title: '%fa:info-circle% %i18n:desktop.banner-updated%',
|
|
|
|
text: null,
|
2018-02-20 18:53:34 +01:00
|
|
|
actions: [{
|
2018-08-06 20:20:26 +02:00
|
|
|
text: '%i18n:common.got-it%'
|
2018-02-20 18:53:34 +01:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
return i;
|
2018-02-20 18:53:34 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
return (file = null) => {
|
|
|
|
const selectedFile = file
|
|
|
|
? Promise.resolve(file)
|
|
|
|
: os.apis.chooseDriveFile({
|
|
|
|
multiple: false,
|
2018-08-06 20:20:26 +02:00
|
|
|
title: '%fa:image% %i18n:desktop.choose-banner%'
|
2018-04-08 17:33:56 +02:00
|
|
|
});
|
2018-04-26 07:44:23 +02:00
|
|
|
|
2018-04-08 17:33:56 +02:00
|
|
|
return selectedFile
|
|
|
|
.then(cropImage)
|
|
|
|
.then(setBanner)
|
|
|
|
.catch(err => err && console.warn(err));
|
|
|
|
};
|
2018-02-20 18:53:34 +01:00
|
|
|
};
|