-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
90 lines (81 loc) · 3.06 KB
/
utils.ts
File metadata and controls
90 lines (81 loc) · 3.06 KB
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
export const storage = {
getToken: (): string | null => {
const token = window.localStorage.getItem('token');
return token && JSON.parse(token);
},
setToken: (token: string) =>
window.localStorage.setItem('token', JSON.stringify(token)),
clearTokens: () => {
window.localStorage.removeItem('token');
window.localStorage.removeItem('expiration');
window.localStorage.removeItem('refresh-token');
},
setExpiration: (expiresIn: number) => {
const expiration = new Date(Date.now() + 1000 * expiresIn);
localStorage.setItem('expiration', expiration.toString());
},
setRefreshExpiration: () => {
// refresh expiration in 3 days
const expiration = new Date(Date.now() + 259200000);
localStorage.setItem('refresh-expiration', expiration.toString());
},
getExpiration: (): string | null => {
return window.localStorage.getItem('expiration');
},
getRefreshExpiration: (): string | null => {
return window.localStorage.getItem('refresh-expiration');
},
getRefreshToken: (): string | null => {
const refreshToken = window.localStorage.getItem('refresh-token');
return refreshToken && JSON.parse(refreshToken);
},
setRefreshToken: (token: string) =>
window.localStorage.setItem('refresh-token', JSON.stringify(token)),
getEmail: (): string | null => {
const email = window.localStorage.getItem('email');
return email && JSON.parse(email);
},
setEmail: (email: string) =>
window.localStorage.setItem('email', JSON.stringify(email)),
};
export const isTokenExpired = (): boolean => {
const storedExpiration = storage.getExpiration();
const expiration = storedExpiration && new Date(storedExpiration);
return expiration ? expiration.getTime() < new Date().getTime() : true;
};
export const isRefreshTokenExpired = (): boolean => {
const storedExpiration = storage.getRefreshExpiration();
const expiration = storedExpiration && new Date(storedExpiration);
return expiration ? expiration.getTime() < new Date().getTime() : true;
};
export const formatBytes = (bytes: number, decimals: number) => {
return (bytes / 1073741824).toFixed(decimals);
};
export const convertToBytes = (gigabyte: number) => {
return Math.round(gigabyte * 1073741824);
};
export const hexToBase64 = (hexString: string) => {
// Step 1: Convert hex to binary
let binaryData = '';
for (let i = 0; i < hexString.length; i += 2) {
binaryData += String.fromCharCode(parseInt(hexString.substr(i, 2), 16));
}
// Step 2: Convert binary to Base64
return btoa(binaryData);
};
export const generateImageHash = async (file: File) => {
const buffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-1', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashValue = hashArray
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
const base64String = hexToBase64(hashValue);
return base64String;
};
export const formatUsedQuotaInGB = (usedQuota: number, decimals = 2) => {
if (usedQuota <= 0) return "0.0";
const rawGB = formatBytes(usedQuota, decimals);
const gbValue = parseFloat(rawGB);
return gbValue < 0.1 ? "0.1" : `${rawGB}`;
}