Retrieve protected files using the defined file model

Adds support to retrieve protected files using the class they are defined to be using if that class differs from the default System\Models\File class. This makes it possible to use a custom class extending the base file model class that does output processing on the file data (for example, an EncryptedFile class that has to decrypt the file contents before they can be output to the browser).
This commit is contained in:
Luke Towers 2018-03-09 23:07:53 -06:00 committed by GitHub
parent 55d49cbf2b
commit f056e8dcb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -110,6 +110,23 @@ class Files extends Controller
if (!$file = FileModel::find((int) $id)) {
throw new ApplicationException('Unable to find file');
}
/**
* Ensure that the file model utilized for this request is
* the one specified in the relationship configuration
*/
if ($file->attachment) {
$fileModel = $file->attachment->{$file->field}()->getRelated();
/**
* Only attempt to get file model through its assigned class
* when the assigned class differs from the default one that
* the file has already been loaded from
*/
if (get_class($file) !== get_class($fileModel)) {
$file = $fileModel->find($file->id);
}
}
$verifyCode = self::getUniqueCode($file);
if ($code != $verifyCode) {