summaryrefslogtreecommitdiff
path: root/src/client/app/desktop/api/update-avatar.ts
blob: f08e8a2b4e44fbfb1d0680a45bc83008f917b6e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import OS from '../../mios';
import { apiUrl } from '../../config';
import CropWindow from '../views/components/crop-window.vue';
import ProgressDialog from '../views/components/progress-dialog.vue';

export default (os: OS) => {

	const cropImage = file => new Promise((resolve, reject) => {

		const regex = RegExp('\.(jpg|jpeg|png|gif|webp|bmp|tiff)$');
		if (!regex.test(file.name) ) {
			os.apis.dialog({
				title: '%fa:info-circle% %i18n:desktop.invalid-filetype%',
				text: null,
				actions: [{
					text: '%i18n:common.got-it%'
				}]
			});
			return reject('invalid-filetype');
		}

		const w = os.new(CropWindow, {
			image: file,
			title: '%i18n:desktop.avatar-crop-title%',
			aspectRatio: 1 / 1
		});

		w.$once('cropped', blob => {
			const data = new FormData();
			data.append('i', os.store.state.i.token);
			data.append('file', blob, file.name + '.cropped.png');

			os.api('drive/folders/find', {
				name: '%i18n:desktop.avatar%'
			}).then(avatarFolder => {
				if (avatarFolder.length === 0) {
					os.api('drive/folders/create', {
						name: '%i18n:desktop.avatar%'
					}).then(iconFolder => {
						resolve(upload(data, iconFolder));
					});
				} else {
					resolve(upload(data, avatarFolder[0]));
				}
			});
		});

		w.$once('skipped', () => {
			resolve(file);
		});

		w.$once('cancelled', reject);

		document.body.appendChild(w.$el);
	});

	const upload = (data, folder) => new Promise((resolve, reject) => {
		const dialog = os.new(ProgressDialog, {
			title: '%i18n:desktop.uploading-avatar%'
		});
		document.body.appendChild(dialog.$el);

		if (folder) data.append('folderId', folder.id);

		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();
			resolve(file);
		};
		xhr.onerror = reject;

		xhr.upload.onprogress = e => {
			if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
		};

		xhr.send(data);
	});

	const setAvatar = file => {
		return os.api('i/update', {
			avatarId: file.id
		}).then(i => {
			os.store.commit('updateIKeyValue', {
				key: 'avatarId',
				value: i.avatarId
			});
			os.store.commit('updateIKeyValue', {
				key: 'avatarUrl',
				value: i.avatarUrl
			});

			os.apis.dialog({
				title: '%fa:info-circle% %i18n:desktop.avatar-updated%',
				text: null,
				actions: [{
					text: '%i18n:common.got-it%'
				}]
			});

			return i;
		});
	};

	return (file = null) => {
		const selectedFile = file
			? Promise.resolve(file)
			: os.apis.chooseDriveFile({
				multiple: false,
				title: '%fa:image% %i18n:desktop.choose-avatar%'
			});

		return selectedFile
			.then(cropImage)
			.then(setAvatar)
			.catch(err => err && console.warn(err));
	};
};