-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: serve public static files from packages
- Loading branch information
Showing
8 changed files
with
274 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
// This file is part of the QuestionPy Moodle plugin - https://questionpy.org | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle 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 the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace qtype_questionpy; | ||
|
||
use coding_exception; | ||
use context_system; | ||
use dml_exception; | ||
use invalid_dataroot_permissions; | ||
use qtype_questionpy\api\api; | ||
|
||
class static_file_service { | ||
|
||
private readonly api $api; | ||
private readonly package_file_service $packagefileservice; | ||
|
||
public function __construct() { | ||
$this->api = new api(); | ||
$this->packagefileservice = new package_file_service(); | ||
} | ||
|
||
/** | ||
* Gets and serves the given static file from the QPy server and dies afterwards. | ||
* | ||
* TODO: Cache the file. | ||
* | ||
* @param string $packagehash | ||
* @param string $namespace | ||
* @param string $shortname | ||
* @param string $path | ||
* @return never | ||
* @throws coding_exception | ||
* @throws dml_exception | ||
* @throws invalid_dataroot_permissions | ||
*/ | ||
public function serve_public_static_file(string $packagehash, string $namespace, string $shortname, string $path): never { | ||
$fileiflocal = $this->packagefileservice->get_file_by_package_hash($packagehash, context_system::instance()->id); | ||
|
||
$temppath = make_request_directory() . "/$packagehash/$namespace/$shortname/$path"; | ||
make_writable_directory(dirname($temppath)); | ||
|
||
$mimetype = $this->api->package($packagehash, $fileiflocal) | ||
->download_static_file($namespace, $shortname, "static", $path, $temppath); | ||
|
||
/* Set a lifetime of 1 year, i.e. effectively never expire. Since the package hash is part of the URL, cache | ||
busting is automatic. */ | ||
send_file($temppath, basename($path), lifetime: 31536000, mimetype: $mimetype, | ||
options: ["immutable" => true, "cacheability" => "public"]); | ||
die; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.