Skip to content

Commit

Permalink
Various fixes to support private asset files and directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdp1337 committed Jun 21, 2017
1 parent 6745967 commit e531693
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 75 deletions.
3 changes: 1 addition & 2 deletions src/core/libs/core/filestore/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ static function Directory($uri){
strpos($uri, 'private/') === 0 ||
strpos($uri, get_private_path()) === 0
){
// @TODO
//return resolve_private_file($uri);
return resolve_private_directory($uri);
}

// Is this a tmp request?
Expand Down
126 changes: 53 additions & 73 deletions src/core/libs/core/filestore/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,53 +429,62 @@ function resolve_asset_file($filename){
* @throws \Exception
*/
function resolve_public_file($filename){
$resolved = get_public_path();

if (strpos($filename, 'public/') === 0) {
// Allow "assets/blah" to be passed in
$filename = substr($filename, 7);
}
elseif(strpos($filename, $resolved) === 0){
// Allow the fully resolved name to be passed in
$filename = substr($filename, strlen($resolved));
}

switch(CDN_TYPE){
case 'local':
if(\Core\ftp()){
// FTP has its own sub-type.
return new Backends\FileFTP($resolved . $filename);
}
else{
return new Backends\FileLocal($resolved . $filename);
}
break;
return _resolve_type_file($filename, get_public_path(), 'public/', 'file');
}

case 'ftp':
return new Backends\FileFTP($resolved . $filename, cdn_ftp());
break;
/**
* Resolve a name for a private to an actual file.
*
* @param $filename
*
* @return \Core\Filestore\File
*
* @throws \Exception
*/
function resolve_private_file($filename){
return _resolve_type_file($filename, get_private_path(), 'private/', 'file');
}

default:
throw new \Exception('Unsupported CDN type: ' . CDN_TYPE);
break;
}
/**
* Resolve a name for a public to an actual file.
*
* @param $filename
*
* @return \Core\Filestore\Directory
*
* @throws \Exception
*/
function resolve_public_directory($filename){
return _resolve_type_file($filename, get_public_path(), 'public/', 'directory');
}

/**
* Resolve a name for a private to an actual file.
*
* @param $filename
*
* @return \Core\Filestore\Directory
*
* @throws \Exception
*/
function resolve_private_directory($filename){
return _resolve_type_file($filename, get_private_path(), 'private/', 'directory');
}

/**
* Resolve a name for a public/private to an actual file.
*
* @param $filename
*
* @return \Core\Filestore\File
*
* @throws \Exception
*/
function resolve_private_file($filename){
$resolved = get_private_path();
function _resolve_type_file($filename, $resolved, $prefix, $type){

if (strpos($filename, 'private/') === 0) {
if (strpos($filename, $prefix) === 0) {
// Allow "assets/blah" to be passed in
$filename = substr($filename, 8);
$filename = substr($filename, strlen($prefix));
}
elseif(strpos($filename, $resolved) === 0){
// Allow the fully resolved name to be passed in
Expand All @@ -486,15 +495,24 @@ function resolve_private_file($filename){
case 'local':
if(\Core\ftp()){
// FTP has its own sub-type.
return new Backends\FileFTP($resolved . $filename);
return
$type === 'file' ?
new Backends\FileFTP($resolved . $filename) :
new Backends\DirectoryFTP($resolved . $filename);
}
else{
return new Backends\FileLocal($resolved . $filename);
return
$type === 'file' ?
new Backends\FileLocal($resolved . $filename) :
new Backends\DirectoryLocal($resolved . $filename);
}
break;

case 'ftp':
return new Backends\FileFTP($resolved . $filename, cdn_ftp());
return
$type === 'file' ?
new Backends\FileFTP($resolved . $filename, cdn_ftp()) :
new Backends\DirectoryFTP($resolved . $filename, cdn_ftp());
break;

default:
Expand Down Expand Up @@ -547,45 +565,7 @@ function resolve_asset_directory($filename){
}


/**
* Resolve a name for a public to an actual file.
*
* @param $filename
*
* @return \Core\Filestore\Directory
*
* @throws \Exception
*/
function resolve_public_directory($filename){
$resolved = get_public_path();

if (strpos($filename, 'public/') === 0) {
// Allow "assets/blah" to be passed in
$filename = substr($filename, 7);
}
elseif(strpos($filename, $resolved) === 0){
// Allow the fully resolved name to be passed in
$filename = substr($filename, strlen($resolved));
}

// I need to check the custom, current theme, and finally default locations for the file.
$theme = \ConfigHandler::Get('/theme/selected');
switch(CDN_TYPE){
case 'local':
if(\Core\ftp()){
// FTP has its own sub-type.
return new Backends\DirectoryFTP($resolved . $filename);
}
else{
return new Backends\DirectoryLocal($resolved . $filename);
}

break;
default:
throw new \Exception('Unsupported CDN type: ' . CDN_TYPE);
break;
}
}

/**
* Function to translate a PHP upload error into a human-readable string.
Expand Down

0 comments on commit e531693

Please sign in to comment.