2018-04-29 21:37:51 +09:00
|
|
|
import OS from '../../mios';
|
2018-02-21 02:53:34 +09:00
|
|
|
import { apiUrl } from '../../config';
|
|
|
|
import CropWindow from '../views/components/crop-window.vue';
|
|
|
|
import ProgressDialog from '../views/components/progress-dialog.vue';
|
|
|
|
|
|
|
|
export default (os: OS) => (cb, file = null) => {
|
|
|
|
const fileSelected = file => {
|
|
|
|
|
2018-05-27 13:49:09 +09:00
|
|
|
const w = os.new(CropWindow, {
|
|
|
|
image: file,
|
2018-08-07 03:20:26 +09:00
|
|
|
title: '%i18n:desktop.avatar-crop-title%',
|
2018-05-27 13:49:09 +09:00
|
|
|
aspectRatio: 1 / 1
|
|
|
|
});
|
2018-02-21 02:53:34 +09:00
|
|
|
|
|
|
|
w.$once('cropped', blob => {
|
|
|
|
const data = new FormData();
|
2018-05-27 13:49:09 +09:00
|
|
|
data.append('i', os.store.state.i.token);
|
2018-02-21 02:53:34 +09:00
|
|
|
data.append('file', blob, file.name + '.cropped.png');
|
|
|
|
|
|
|
|
os.api('drive/folders/find', {
|
2018-08-07 03:20:26 +09:00
|
|
|
name: '%i18n:desktop.avatar%'
|
2018-02-21 02:53:34 +09:00
|
|
|
}).then(iconFolder => {
|
|
|
|
if (iconFolder.length === 0) {
|
|
|
|
os.api('drive/folders/create', {
|
2018-08-07 03:20:26 +09:00
|
|
|
name: '%i18n:desktop.avatar%'
|
2018-02-21 02:53:34 +09:00
|
|
|
}).then(iconFolder => {
|
|
|
|
upload(data, iconFolder);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
upload(data, iconFolder[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
w.$once('skipped', () => {
|
|
|
|
set(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(w.$el);
|
|
|
|
};
|
|
|
|
|
|
|
|
const upload = (data, folder) => {
|
2018-05-27 13:49:09 +09:00
|
|
|
const dialog = os.new(ProgressDialog, {
|
2018-08-07 03:20:26 +09:00
|
|
|
title: '%i18n:desktop.uploading-avatar%'
|
2018-05-27 13:49:09 +09:00
|
|
|
});
|
2018-02-21 02:53:34 +09:00
|
|
|
document.body.appendChild(dialog.$el);
|
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
if (folder) data.append('folderId', folder.id);
|
2018-02-21 02:53:34 +09: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();
|
|
|
|
set(file);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
2018-02-21 05:55:19 +09:00
|
|
|
if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
|
2018-02-21 02:53:34 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const set = file => {
|
|
|
|
os.api('i/update', {
|
2018-03-29 14:48:47 +09:00
|
|
|
avatarId: file.id
|
2018-02-21 02:53:34 +09:00
|
|
|
}).then(i => {
|
2018-05-27 13:49:09 +09:00
|
|
|
os.store.commit('updateIKeyValue', {
|
|
|
|
key: 'avatarId',
|
|
|
|
value: i.avatarId
|
|
|
|
});
|
|
|
|
os.store.commit('updateIKeyValue', {
|
|
|
|
key: 'avatarUrl',
|
|
|
|
value: i.avatarUrl
|
|
|
|
});
|
2018-02-21 05:55:19 +09:00
|
|
|
|
2018-02-21 02:53:34 +09:00
|
|
|
os.apis.dialog({
|
2018-08-07 03:20:26 +09:00
|
|
|
title: '%fa:info-circle% %i18n:desktop.avatar-updated%',
|
|
|
|
text: null,
|
2018-02-21 02:53:34 +09:00
|
|
|
actions: [{
|
2018-08-07 03:20:26 +09:00
|
|
|
text: '%i18n:common.got-it%'
|
2018-02-21 02:53:34 +09:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cb) cb(i);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
fileSelected(file);
|
|
|
|
} else {
|
|
|
|
os.apis.chooseDriveFile({
|
|
|
|
multiple: false,
|
2018-08-07 03:20:26 +09:00
|
|
|
title: '%fa:image% %i18n:desktop.choose-avatar%'
|
2018-02-21 02:53:34 +09:00
|
|
|
}).then(file => {
|
|
|
|
fileSelected(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|