forked from drdrew42/renderer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtils.pm
154 lines (128 loc) · 5.12 KB
/
Utils.pm
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2022 The WeBWorK Project, https://github.com/openwebwork
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
package WeBWorK::Utils;
use base qw(Exporter);
use strict;
use warnings;
use JSON;
our @EXPORT_OK = qw(
wwRound
getAssetURL
);
# usage wwRound($places,$float)
# return $float rounded up to number of decimal places given by $places
sub wwRound(@) {
my $places = shift;
my $float = shift;
my $factor = 10**$places;
return int($float * $factor + 0.5) / $factor;
}
my $staticWWAssets;
my $staticPGAssets;
my $thirdPartyWWDependencies;
my $thirdPartyPGDependencies;
sub readJSON {
my $fileName = shift;
return unless -r $fileName;
open(my $fh, "<:encoding(UTF-8)", $fileName) or die "FATAL: Unable to open '$fileName'!";
local $/;
my $data = <$fh>;
close $fh;
return JSON->new->decode($data);
}
sub getThirdPartyAssetURL {
my ($file, $dependencies, $baseURL, $useCDN) = @_;
for (keys %$dependencies) {
if ($file =~ /^node_modules\/$_\/(.*)$/) {
if ($useCDN && $1 !~ /mathquill/) {
return
"https://cdn.jsdelivr.net/npm/$_\@"
. substr($dependencies->{$_}, 1) . '/'
. ($1 =~ s/(?:\.min)?\.(js|css)$/.min.$1/gr);
} else {
return "$baseURL/$file?version=" . ($dependencies->{$_} =~ s/#/@/gr);
}
}
}
return;
}
# Get the url for static assets.
sub getAssetURL {
my ($language, $file) = @_;
# Load the static files list generated by `npm install` the first time this method is called.
unless ($staticWWAssets) {
my $staticAssetsList = "$ENV{RENDER_ROOT}/public/static-assets.json";
$staticWWAssets = readJSON($staticAssetsList);
unless ($staticWWAssets) {
warn "ERROR: '$staticAssetsList' not found or not readable!\n"
. "You may need to run 'npm install' from '$ENV{RENDER_ROOT}/public'.";
$staticWWAssets = {};
}
}
unless ($staticPGAssets) {
my $staticAssetsList = "$ENV{PG_ROOT}/htdocs/static-assets.json";
$staticPGAssets = readJSON($staticAssetsList);
unless ($staticPGAssets) {
warn "ERROR: '$staticAssetsList' not found or not readable!\n"
. "You may need to run 'npm install' from '$ENV{PG_ROOT}/htdocs'.";
$staticPGAssets = {};
}
}
unless ($thirdPartyWWDependencies) {
my $packageJSON = "$ENV{RENDER_ROOT}/public/package.json";
my $data = readJSON($packageJSON);
warn "ERROR: '$packageJSON' not found or not readable!\n" unless $data && defined $data->{dependencies};
$thirdPartyWWDependencies = $data->{dependencies} // {};
}
unless ($thirdPartyPGDependencies) {
my $packageJSON = "$ENV{PG_ROOT}/htdocs/package.json";
my $data = readJSON($packageJSON);
warn "ERROR: '$packageJSON' not found or not readable!\n" unless $data && defined $data->{dependencies};
$thirdPartyPGDependencies = $data->{dependencies} // {};
}
# Check to see if this is a third party asset file in node_modules (either in webwork2/htdocs or pg/htdocs).
# If so, then either serve it from a CDN if requested, or serve it directly with the library version
# appended as a URL parameter.
if ($file =~ /^node_modules/) {
my $wwFile = getThirdPartyAssetURL(
$file, $thirdPartyWWDependencies,
'',
0
);
return $wwFile if $wwFile;
my $pgFile =
getThirdPartyAssetURL($file, $thirdPartyPGDependencies, '/pg_files', 1);
return $pgFile if $pgFile;
}
# If a right-to-left language is enabled (Hebrew or Arabic) and this is a css file that is not a third party asset,
# then determine the rtl varaint file name. This will be looked for first in the asset lists.
my $rtlfile =
($language =~ /^(he|ar)/ && $file !~ /node_modules/ && $file =~ /\.css$/)
? $file =~ s/\.css$/.rtl.css/r
: undef;
# First check to see if this is a file in the webwork htdocs location with a rtl variant.
return "/$staticWWAssets->{$rtlfile}"
if defined $rtlfile && defined $staticWWAssets->{$rtlfile};
# Next check to see if this is a file in the webwork htdocs location.
return "/$staticWWAssets->{$file}" if defined $staticWWAssets->{$file};
# Now check to see if this is a file in the pg htdocs location with a rtl variant.
return "/pg_files/$staticPGAssets->{$rtlfile}" if defined $rtlfile && defined $staticPGAssets->{$rtlfile};
# Next check to see if this is a file in the pg htdocs location.
return "/pg_files/$staticPGAssets->{$file}" if defined $staticPGAssets->{$file};
# If the file was not found in the lists, then just use the given file and assume its path is relative to the
# render app public folder.
return "/$file";
}
1;