forked from hackclub/construct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
87 lines (74 loc) · 2.33 KB
/
utils.ts
File metadata and controls
87 lines (74 loc) · 2.33 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
export function isValidUrl(string: string) {
try {
new URL(string);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
return false;
}
}
export const projectStatuses = {
building: 'Building',
submitted: 'Submitted',
t1_approved: 'On print queue',
printing: 'Being printed',
printed: 'Printed',
t2_approved: 'Approved',
finalized: 'Finalized',
rejected: 'Rejected',
rejected_locked: 'Rejected (locked)'
};
export default function fileSizeFromUrl(url: string): Promise<number> {
return new Promise((resolve, reject) => {
if (!url) {
return reject(new Error('Invalid URL'));
}
fetch(url, { method: 'HEAD' })
.then((response) => {
if (!response.ok) {
return reject(new Error(`Failed to get file size, status code: ${response.status}`));
}
const contentLength = response.headers.get('content-length');
if (!contentLength) {
return reject(new Error("Couldn't retrieve file size from headers"));
}
const size: number = parseInt(contentLength, 10);
if (isNaN(size)) {
return reject(new Error("Couldn't retrieve file size from headers"));
}
resolve(size);
})
.catch((err) => {
reject(err);
});
});
}
export function formatMinutes(mins: number | null) {
return Math.floor((mins ?? 0) / 60) + 'h ' + Math.floor((mins ?? 0) % 60) + 'min';
}
export function calculateMarketPrice(
minPrice: number,
maxPrice: number,
minShopScore: number,
maxShopScore: number,
userShopScore: number
) {
if (userShopScore <= minShopScore) {
return maxPrice;
} else if (userShopScore >= maxShopScore) {
return minPrice;
} else {
const priceDiff = maxPrice - minPrice;
const shopScoreDiff = maxShopScore - minShopScore;
const m = priceDiff / shopScoreDiff; // diff_y/diff_x
const shopScoreRemainder = userShopScore - minShopScore;
// y = -mx + c
return Math.round(-m * shopScoreRemainder + maxPrice);
}
}
export function getProjectLinkType(editorFileType: string | null, editorUrl: string | null, uploadedFileUrl: string | null): string {
if (editorFileType === 'url' && editorUrl?.includes('cad.onshape.com')) return 'onshape';
if (editorFileType === 'url' && editorUrl?.includes('autodesk360.com')) return 'fusion-link';
if (editorFileType === 'upload' && uploadedFileUrl?.endsWith('.f3d')) return 'fusion-file';
return 'unknown';
}