2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets;
|
|
|
|
|
|
|
|
use SilverStripe\Admin\AdminRootController;
|
|
|
|
use SilverStripe\Admin\CMSPreviewable;
|
|
|
|
use SilverStripe\Assets\Storage\DBFile;
|
|
|
|
use SilverStripe\Assets\Storage\AssetContainer;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Forms\DatetimeField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\HeaderField;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
2016-09-07 05:35:47 +02:00
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
|
|
|
use InvalidArgumentException;
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* This class handles the representation of a file on the filesystem within the framework.
|
|
|
|
* Most of the methods also handle the {@link Folder} subclass.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* Note: The files are stored in the assets/ directory, but SilverStripe
|
2007-07-19 12:40:28 +02:00
|
|
|
* looks at the db object to gather information about a file such as URL
|
2010-10-13 01:02:10 +02:00
|
|
|
* It then uses this for all processing functions (like image manipulation).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Security</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* Caution: It is recommended to disable any script execution in the "assets/"
|
|
|
|
* directory in the webserver configuration, to reduce the risk of exploits.
|
|
|
|
* See http://doc.silverstripe.org/secure-development#filesystem
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* <b>Asset storage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* As asset storage is configured separately to any File DataObject records, this class
|
|
|
|
* does not make any assumptions about how these records are saved. They could be on
|
|
|
|
* a local filesystem, remote filesystem, or a virtual record container (such as in local memory).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* The File dataobject simply represents an externally facing view of shared resources
|
|
|
|
* within this asset store.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* Internally individual files are referenced by a"Filename" parameter, which represents a File, extension,
|
|
|
|
* and is optionally prefixed by a list of custom directories. This path is root-agnostic, so it does not
|
|
|
|
* automatically have a direct url mapping (even to the site's base directory).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* Additionally, individual files may have several versions distinguished by sha1 hash,
|
|
|
|
* of which a File DataObject can point to a single one. Files can also be distinguished by
|
|
|
|
* variants, which may be resized images or format-shifted documents.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* <b>Properties</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Title": Optional title of the file (for display purposes only).
|
2011-03-23 01:23:43 +01:00
|
|
|
* Defaults to "Name". Note that the Title field of Folder (subclass of File)
|
2015-09-15 04:52:02 +02:00
|
|
|
* is linked to Name, so Name and Title will always be the same.
|
|
|
|
* -"File": Physical asset backing this DB record. This is a composite DB field with
|
|
|
|
* its own list of properties. {@see DBFile} for more information
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Content": Typically unused, but handy for a textual representation of
|
2015-09-15 04:52:02 +02:00
|
|
|
* files, e.g. for fulltext indexing of PDF documents.
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "ParentID": Points to a {@link Folder} record. Should be in sync with
|
|
|
|
* "Filename". A ParentID=0 value points to the "assets/" folder, not the webroot.
|
2015-09-15 04:52:02 +02:00
|
|
|
* -"ShowInSearch": True if this file is searchable
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @property string $Name Basename of the file
|
|
|
|
* @property string $Title Title of the file
|
|
|
|
* @property DBFile $File asset stored behind this File record
|
|
|
|
* @property string $Content
|
|
|
|
* @property string $ShowInSearch Boolean that indicates if file is shown in search. Doesn't apply to Folders
|
|
|
|
* @property int $ParentID ID of parent File/Folder
|
|
|
|
* @property int $OwnerID ID of Member who owns the file
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @method File Parent() Returns parent File
|
|
|
|
* @method Member Owner() Returns Member object of file owner.
|
2016-01-26 04:56:07 +01:00
|
|
|
*
|
|
|
|
* @mixin Hierarchy
|
|
|
|
* @mixin Versioned
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-04-27 04:26:39 +02:00
|
|
|
class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumbnail, CMSPreviewable {
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
use ImageManipulation;
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
private static $default_sort = "\"Name\"";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-26 04:56:07 +01:00
|
|
|
private static $singular_name = "File";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
private static $plural_name = "Files";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permissions necessary to view files outside of the live stage (e.g. archive / draft stage).
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_AssetAdmin', 'VIEW_DRAFT_CONTENT');
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"Name" => "Varchar(255)",
|
|
|
|
"Title" => "Varchar(255)",
|
2015-09-15 04:52:02 +02:00
|
|
|
"File" =>"DBFile",
|
2011-09-15 16:00:54 +02:00
|
|
|
// Only applies to files, doesn't inherit for folder
|
|
|
|
'ShowInSearch' => 'Boolean(1)',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2016-08-19 00:51:35 +02:00
|
|
|
"Parent" => "SilverStripe\\Assets\\File",
|
2016-06-23 01:37:22 +02:00
|
|
|
"Owner" => "SilverStripe\\Security\\Member"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $defaults = array(
|
2011-09-15 16:00:54 +02:00
|
|
|
"ShowInSearch" => 1,
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
"SilverStripe\\ORM\\Hierarchy\\Hierarchy",
|
|
|
|
"SilverStripe\\ORM\\Versioning\\Versioned"
|
2009-02-02 00:49:53 +01:00
|
|
|
);
|
2014-03-27 04:24:11 +01:00
|
|
|
|
|
|
|
private static $casting = array (
|
2016-06-03 10:51:02 +02:00
|
|
|
'TreeTitle' => 'HTMLFragment'
|
2014-03-27 04:24:11 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
private static $table_name = 'File';
|
|
|
|
|
2010-04-12 04:35:12 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-15 05:04:27 +02:00
|
|
|
* @var array List of allowed file extensions, enforced through {@link validate()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-05 01:05:05 +01:00
|
|
|
* Note: if you modify this, you should also change a configuration file in the assets directory.
|
|
|
|
* Otherwise, the files will be able to be uploaded but they won't be able to be served by the
|
|
|
|
* webserver.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-04 21:57:31 +01:00
|
|
|
* - If you are running Apache you will need to change assets/.htaccess
|
2014-08-15 08:53:05 +02:00
|
|
|
* - If you are running IIS you will need to change assets/web.config
|
2010-12-05 01:05:05 +01:00
|
|
|
*
|
|
|
|
* Instructions for the change you need to make are included in a comment in the config file.
|
2010-04-12 04:35:12 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_extensions = array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'', 'ace', 'arc', 'arj', 'asf', 'au', 'avi', 'bmp', 'bz2', 'cab', 'cda', 'css', 'csv', 'dmg', 'doc',
|
|
|
|
'docx', 'dotx', 'dotm', 'flv', 'gif', 'gpx', 'gz', 'hqx', 'ico', 'jar', 'jpeg', 'jpg', 'js', 'kml',
|
|
|
|
'm4a', 'm4v', 'mid', 'midi', 'mkv', 'mov', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'ogg', 'ogv', 'pages',
|
|
|
|
'pcx', 'pdf', 'png', 'pps', 'ppt', 'pptx', 'potx', 'potm', 'ra', 'ram', 'rm', 'rtf', 'sit', 'sitx',
|
|
|
|
'tar', 'tgz', 'tif', 'tiff', 'txt', 'wav', 'webm', 'wma', 'wmv', 'xls', 'xlsx', 'xltx', 'xltm', 'zip',
|
|
|
|
'zipx',
|
2010-04-12 04:42:51 +02:00
|
|
|
);
|
2012-02-23 22:43:37 +01:00
|
|
|
|
2012-03-02 17:30:48 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2012-03-02 17:30:48 +01:00
|
|
|
* @var array Category identifiers mapped to commonly used extensions.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $app_categories = array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'archive' => array(
|
|
|
|
'ace', 'arc', 'arj', 'bz', 'bz2', 'cab', 'dmg', 'gz', 'hqx', 'jar', 'rar', 'sit', 'sitx', 'tar', 'tgz',
|
|
|
|
'zip', 'zipx',
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
2015-09-15 04:52:02 +02:00
|
|
|
'audio' => array(
|
|
|
|
'aif', 'aifc', 'aiff', 'apl', 'au', 'avr', 'cda', 'm4a', 'mid', 'midi', 'mp3', 'ogg', 'ra',
|
|
|
|
'ram', 'rm', 'snd', 'wav', 'wma',
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
2015-09-15 04:52:02 +02:00
|
|
|
'document' => array(
|
|
|
|
'css', 'csv', 'doc', 'docx', 'dotm', 'dotx', 'htm', 'html', 'gpx', 'js', 'kml', 'pages', 'pdf',
|
|
|
|
'potm', 'potx', 'pps', 'ppt', 'pptx', 'rtf', 'txt', 'xhtml', 'xls', 'xlsx', 'xltm', 'xltx', 'xml',
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
|
|
|
'image' => array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'alpha', 'als', 'bmp', 'cel', 'gif', 'ico', 'icon', 'jpeg', 'jpg', 'pcx', 'png', 'ps', 'tif', 'tiff',
|
|
|
|
),
|
|
|
|
'image/supported' => array(
|
|
|
|
'gif', 'jpeg', 'jpg', 'png'
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
|
|
|
'flash' => array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'fla', 'swf'
|
|
|
|
),
|
|
|
|
'video' => array(
|
|
|
|
'asf', 'avi', 'flv', 'ifo', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mp2', 'mp4', 'mpa', 'mpe', 'mpeg',
|
|
|
|
'mpg', 'ogv', 'qt', 'vob', 'webm', 'wmv',
|
2012-09-04 01:08:38 +02:00
|
|
|
),
|
2015-09-15 04:52:02 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of file extensions to class type
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private static $class_for_file_extension = array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'*' => 'SilverStripe\\Assets\\File',
|
|
|
|
'jpg' => 'SilverStripe\\Assets\\Image',
|
|
|
|
'jpeg' => 'SilverStripe\\Assets\\Image',
|
|
|
|
'png' => 'SilverStripe\\Assets\\Image',
|
|
|
|
'gif' => 'SilverStripe\\Assets\\Image',
|
2012-03-02 17:30:48 +01:00
|
|
|
);
|
|
|
|
|
2010-04-12 04:35:12 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2016-06-23 01:37:22 +02:00
|
|
|
* @var bool If this is true, then restrictions set in {@link $allowed_max_file_size} and
|
2010-10-15 05:04:27 +02:00
|
|
|
* {@link $allowed_extensions} will be applied to users with admin privileges as
|
2010-04-12 04:35:12 +02:00
|
|
|
* well.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $apply_restrictions_to_admin = true;
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* If enabled, legacy file dataobjects will be automatically imported into the APL
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2015-09-15 04:52:02 +02:00
|
|
|
* @var bool
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
private static $migrate_legacy_file = false;
|
2012-03-13 23:01:02 +01:00
|
|
|
|
2008-03-07 04:19:49 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* @config
|
|
|
|
* @var boolean
|
2008-03-07 04:19:49 +01:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
private static $update_filesystem = true;
|
|
|
|
|
|
|
|
public static function get_shortcodes() {
|
|
|
|
return 'file_link';
|
|
|
|
}
|
2012-03-01 09:13:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace "[file_link id=n]" shortcode with an anchor tag or link to the file.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param array $arguments Arguments passed to the parser
|
|
|
|
* @param string $content Raw shortcode
|
|
|
|
* @param ShortcodeParser $parser Parser
|
|
|
|
* @param string $shortcode Name of shortcode used to register this handler
|
|
|
|
* @param array $extra Extra arguments
|
|
|
|
* @return string Result of the handled shortcode
|
2012-03-01 09:13:42 +01:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) {
|
2016-03-08 12:20:51 +01:00
|
|
|
// Find appropriate record, with fallback for error handlers
|
|
|
|
$record = static::find_shortcode_record($arguments, $errorCode);
|
2016-01-26 04:56:07 +01:00
|
|
|
if($errorCode) {
|
2016-03-08 12:20:51 +01:00
|
|
|
$record = static::find_error_record($errorCode);
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
if (!$record) {
|
|
|
|
return null; // There were no suitable matches at all.
|
2012-03-01 09:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the HTML tag
|
|
|
|
if($content) {
|
|
|
|
// build some useful meta-data (file type and size) as data attributes
|
|
|
|
$attrs = ' ';
|
|
|
|
if($record instanceof File) {
|
|
|
|
foreach(array(
|
|
|
|
'class' => 'file',
|
|
|
|
'data-type' => $record->getExtension(),
|
|
|
|
'data-size' => $record->getSize()
|
|
|
|
) as $name => $value) {
|
|
|
|
$attrs .= sprintf('%s="%s" ', $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('<a href="%s"%s>%s</a>', $record->Link(), rtrim($attrs), $parser->parse($content));
|
|
|
|
} else {
|
|
|
|
return $record->Link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
/**
|
|
|
|
* Find the record to use for a given shortcode.
|
|
|
|
*
|
|
|
|
* @param array $args Array of input shortcode arguments
|
|
|
|
* @param int $errorCode If the file is not found, or is inaccessible, this will be assigned to a HTTP error code.
|
|
|
|
* @return File|null The File DataObject, if it can be found.
|
|
|
|
*/
|
|
|
|
public static function find_shortcode_record($args, &$errorCode = null) {
|
|
|
|
// Validate shortcode
|
|
|
|
if(!isset($args['id']) || !is_numeric($args['id'])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file is found
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var File $file */
|
2016-03-08 12:20:51 +01:00
|
|
|
$file = File::get()->byID($args['id']);
|
|
|
|
if (!$file) {
|
|
|
|
$errorCode = 404;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file is viewable
|
|
|
|
if(!$file->canView()) {
|
|
|
|
$errorCode = 403;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a HTTP Error, find an appropriate substitute File or SiteTree data object instance.
|
|
|
|
*
|
|
|
|
* @param int $errorCode HTTP Error value
|
|
|
|
* @return File|SiteTree File or SiteTree object to use for the given error
|
|
|
|
*/
|
|
|
|
protected static function find_error_record($errorCode) {
|
|
|
|
$result = static::singleton()->invokeWithExtensions('getErrorRecordFor', $errorCode);
|
|
|
|
$result = array_filter($result);
|
|
|
|
if($result) {
|
|
|
|
return reset($result);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
/**
|
|
|
|
* A file only exists if the file_exists() and is in the DB as a record
|
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* Use $file->isInDB() to only check for a DB record
|
|
|
|
* Use $file->File->exists() to only check if the asset exists
|
|
|
|
*
|
2015-04-02 12:27:33 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists() {
|
2015-09-15 04:52:02 +02:00
|
|
|
return parent::exists() && $this->File->exists();
|
2015-04-02 12:27:33 +02:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Find a File object by the given filename.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param string $filename Filename to search for, including any custom parent directories.
|
|
|
|
* @return File
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function find($filename) {
|
2010-10-15 05:12:39 +02:00
|
|
|
// Split to folders and the actual filename, and traverse the structure.
|
2008-12-04 23:38:32 +01:00
|
|
|
$parts = explode("/", $filename);
|
|
|
|
$parentID = 0;
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var File $item */
|
2008-12-04 23:38:32 +01:00
|
|
|
$item = null;
|
|
|
|
foreach($parts as $part) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$item = File::get()->filter(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Name' => $part,
|
2013-06-21 00:32:08 +02:00
|
|
|
'ParentID' => $parentID
|
|
|
|
))->first();
|
2008-12-04 23:38:32 +01:00
|
|
|
if(!$item) break;
|
|
|
|
$parentID = $item->ID;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $item;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-20 12:14:46 +02:00
|
|
|
/**
|
|
|
|
* Just an alias function to keep a consistent API with SiteTree
|
|
|
|
*
|
|
|
|
* @return string The link to the file
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2014-08-20 12:14:46 +02:00
|
|
|
return $this->getURL();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2014-08-20 12:14:46 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* @deprecated 4.0
|
2014-08-20 12:14:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RelativeLink() {
|
2015-09-15 04:52:02 +02:00
|
|
|
Deprecation::notice('4.0', 'Use getURL instead, as not all files will be relative to the site root.');
|
|
|
|
return Director::makeRelative($this->getURL());
|
2014-08-20 12:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just an alias function to keep a consistent API with SiteTree
|
|
|
|
*
|
|
|
|
* @return string The absolute link to the file
|
|
|
|
*/
|
|
|
|
public function AbsoluteLink() {
|
|
|
|
return $this->getAbsoluteURL();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2012-03-01 09:13:42 +01:00
|
|
|
|
2010-12-16 06:16:06 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTreeTitle() {
|
2012-05-03 17:41:02 +02:00
|
|
|
return Convert::raw2xml($this->Title);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param Member $member
|
2016-01-26 04:56:07 +01:00
|
|
|
* @return bool
|
2008-11-07 13:17:42 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canView($member = null) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$result = $this->extendedCan('canView', $member);
|
|
|
|
if($result !== null) {
|
|
|
|
return $result;
|
2016-04-29 07:50:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Check if this file can be modified
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param Member $member
|
2008-11-07 13:17:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canEdit($member = null) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-01-11 05:40:04 +01:00
|
|
|
$result = $this->extendedCan('canEdit', $member);
|
2015-09-15 04:52:02 +02:00
|
|
|
if($result !== null) {
|
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-20 08:02:08 +02:00
|
|
|
return Permission::checkMember($member, array('CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain'));
|
2008-11-07 13:17:42 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Check if a file can be created
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @param array $context
|
2008-11-07 13:17:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-06-09 01:31:07 +02:00
|
|
|
public function canCreate($member = null, $context = array()) {
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-09 01:31:07 +02:00
|
|
|
$result = $this->extendedCan('canCreate', $member, $context);
|
|
|
|
if($result !== null) {
|
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Check if this file can be deleted
|
|
|
|
*
|
|
|
|
* @param Member $member
|
2008-11-07 13:17:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canDelete($member = null) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$result = $this->extendedCan('canDelete', $member);
|
|
|
|
if($result !== null) {
|
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2012-01-09 17:14:40 +01:00
|
|
|
|
2012-02-28 18:07:55 +01:00
|
|
|
/**
|
2012-03-30 16:55:35 +02:00
|
|
|
* Returns the fields to power the edit screen of files in the CMS.
|
|
|
|
* You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension}
|
2016-08-31 12:50:43 +02:00
|
|
|
* and implementing updateCMSFields(FieldList $fields) on that extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-28 18:07:55 +01:00
|
|
|
* @return FieldList
|
2012-02-23 22:43:37 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2016-08-31 12:50:43 +02:00
|
|
|
$path = '/' . dirname($this->getFilename());
|
|
|
|
|
2016-09-19 03:02:26 +02:00
|
|
|
$previewLink = Convert::raw2att($this->PreviewLink());
|
2016-09-07 05:35:47 +02:00
|
|
|
$image = "<img src=\"{$previewLink}\" class=\"editor__thumbnail\" />";
|
2016-10-04 06:09:11 +02:00
|
|
|
|
|
|
|
$statusTitle = $this->getStatusTitle();
|
|
|
|
$statusFlag = ($statusTitle) ? "<span class=\"editor__status-flag\">{$statusTitle}</span>" : '';
|
2016-09-07 05:35:47 +02:00
|
|
|
|
|
|
|
$content = Tab::create('Main',
|
|
|
|
HeaderField::create('TitleHeader', $this->Title, 1)
|
|
|
|
->addExtraClass('editor__heading'),
|
2016-10-04 06:09:11 +02:00
|
|
|
LiteralField::create('StatusFlag', $statusFlag),
|
2016-09-07 05:35:47 +02:00
|
|
|
LiteralField::create("IconFull", $image)
|
|
|
|
->addExtraClass('editor__file-preview'),
|
|
|
|
TabSet::create('Editor',
|
|
|
|
Tab::create('Details',
|
|
|
|
TextField::create("Title", $this->fieldLabel('Title')),
|
|
|
|
TextField::create("Name", $this->fieldLabel('Filename')),
|
|
|
|
ReadonlyField::create(
|
|
|
|
"Path",
|
|
|
|
_t('AssetTableField.PATH', 'Path'),
|
|
|
|
(($path !== '/.') ? $path : '') . '/'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Tab::create('Usage',
|
2016-09-20 02:56:45 +02:00
|
|
|
DatetimeField::create(
|
|
|
|
"Created",
|
|
|
|
_t('AssetTableField.CREATED', 'First uploaded')
|
|
|
|
)->setReadonly(true),
|
2016-09-07 05:35:47 +02:00
|
|
|
DatetimeField::create(
|
|
|
|
"LastEdited",
|
|
|
|
_t('AssetTableField.LASTEDIT', 'Last changed')
|
|
|
|
)->setReadonly(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
HiddenField::create('ID', $this->ID)
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields = FieldList::create(TabSet::create('Root', $content));
|
2012-03-30 16:55:35 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->extend('updateCMSFields', $fields);
|
2016-08-31 12:50:43 +02:00
|
|
|
|
2012-03-07 04:40:24 +01:00
|
|
|
return $fields;
|
2012-01-09 17:14:40 +01:00
|
|
|
}
|
2016-10-04 06:09:11 +02:00
|
|
|
|
|
|
|
protected function getStatusTitle() {
|
|
|
|
$statusTitle = '';
|
|
|
|
if ($this->getIsAddedToStage()) {
|
|
|
|
$statusTitle = _t('File.DRAFT', 'Draft');
|
|
|
|
} elseif ($this->getIsModifiedOnStage()) {
|
|
|
|
$statusTitle = _t('File.MODIFIED', 'Modified');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $statusTitle;
|
|
|
|
}
|
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns a category based on the file extension.
|
|
|
|
* This can be useful when grouping files by type,
|
|
|
|
* showing icons on filelinks, etc.
|
|
|
|
* Possible group values are: "audio", "mov", "zip", "image".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param string $ext Extension to check
|
|
|
|
* @return string
|
2010-10-15 05:12:39 +02:00
|
|
|
*/
|
2012-06-25 17:13:18 +02:00
|
|
|
public static function get_app_category($ext) {
|
|
|
|
$ext = strtolower($ext);
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach(static::config()->app_categories as $category => $exts) {
|
2012-03-02 17:30:48 +01:00
|
|
|
if(in_array($ext, $exts)) return $category;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-02 17:30:48 +01:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-25 17:13:18 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* For a category or list of categories, get the list of file extensions
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param array|string $categories List of categories, or single category
|
|
|
|
* @return array
|
2012-06-25 17:13:18 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public static function get_category_extensions($categories) {
|
|
|
|
if(empty($categories)) {
|
|
|
|
return array();
|
2012-06-25 17:13:18 +02:00
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Fix arguments into a single array
|
|
|
|
if(!is_array($categories)) {
|
|
|
|
$categories = array($categories);
|
|
|
|
} elseif(count($categories) === 1 && is_array(reset($categories))) {
|
|
|
|
$categories = reset($categories);
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Check configured categories
|
|
|
|
$appCategories = self::config()->app_categories;
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Merge all categories into list of extensions
|
|
|
|
$extensions = array();
|
|
|
|
foreach(array_filter($categories) as $category) {
|
|
|
|
if(isset($appCategories[$category])) {
|
|
|
|
$extensions = array_merge($extensions, $appCategories[$category]);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Unknown file category: $category");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$extensions = array_unique($extensions);
|
|
|
|
sort($extensions);
|
|
|
|
return $extensions;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Returns a category based on the file extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function appCategory() {
|
|
|
|
return self::get_app_category($this->getExtension());
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Should be called after the file was uploaded
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterUpload() {
|
2009-04-29 01:52:15 +02:00
|
|
|
$this->extend('onAfterUpload');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-19 06:00:57 +02:00
|
|
|
* Make sure the file has a name
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeWrite() {
|
2012-02-07 18:32:04 +01:00
|
|
|
// Set default owner
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->isInDB() && !$this->OwnerID) {
|
|
|
|
$this->OwnerID = Member::currentUserID();
|
2013-06-14 18:30:09 +02:00
|
|
|
}
|
2012-02-07 18:32:04 +01:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Set default name
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->getField('Name')) {
|
|
|
|
$this->Name ="new-" . strtolower($this->class);
|
2010-10-19 06:00:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Propegate changes to the AssetStore and update the DBFile field
|
2010-10-15 05:20:47 +02:00
|
|
|
$this->updateFilesystem();
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* This will check if the parent record and/or name do not match the name on the underlying
|
|
|
|
* DBFile record, and if so, copy this file to the new location, and update the record to
|
|
|
|
* point to this new file.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* This method will update the File {@see DBFile} field value on success, so it must be called
|
|
|
|
* before writing to the database
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* @return bool True if changed
|
2010-10-15 05:20:47 +02:00
|
|
|
*/
|
|
|
|
public function updateFilesystem() {
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$this->config()->update_filesystem) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Check the file exists
|
|
|
|
if(!$this->File->exists()) {
|
|
|
|
return false;
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2012-03-13 23:01:02 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Avoid moving files on live; Rely on this being done on stage prior to publish.
|
|
|
|
if(Versioned::get_stage() !== Versioned::DRAFT) {
|
|
|
|
return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Check path updated record will point to
|
|
|
|
// If no changes necessary, skip
|
|
|
|
$pathBefore = $this->File->getFilename();
|
2016-03-02 06:18:10 +01:00
|
|
|
$pathAfter = $this->generateFilename();
|
2015-09-15 04:52:02 +02:00
|
|
|
if($pathAfter === $pathBefore) {
|
|
|
|
return false;
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Copy record to new location via stream
|
|
|
|
$stream = $this->File->getStream();
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->File->setFromStream($stream, $pathAfter);
|
2015-10-15 00:09:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:19:32 +02:00
|
|
|
* Collate selected descendants of this page.
|
2007-07-19 12:40:28 +02:00
|
|
|
* $condition will be evaluated on each descendant, and if it is succeeds, that item will be added
|
|
|
|
* to the $collator array.
|
2016-03-02 06:18:10 +01:00
|
|
|
*
|
|
|
|
* @param string $condition The PHP condition to be evaluated. The page will be called $item
|
|
|
|
* @param array $collator An array, passed by reference, to collect all of the matching descendants.
|
|
|
|
* @return true|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function collateDescendants($condition, &$collator) {
|
|
|
|
if($children = $this->Children()) {
|
|
|
|
foreach($children as $item) {
|
2016-06-03 10:51:02 +02:00
|
|
|
/** @var File $item */
|
|
|
|
if(!$condition || eval("return $condition;")) {
|
|
|
|
$collator[] = $item;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$item->collateDescendants($condition, $collator);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2007-09-14 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Setter function for Name. Automatically sets a default title,
|
2010-10-15 05:20:47 +02:00
|
|
|
* and removes characters that might be invalid on the filesystem.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Also adds a suffix to the name if the filename already exists
|
|
|
|
* on the filesystem, and is associated to a different {@link File} database record
|
|
|
|
* in the same folder. This means "myfile.jpg" might become "myfile-1.jpg".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-03-02 06:18:10 +01:00
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$oldName = $this->Name;
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// It can't be blank, default to Title
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$name) {
|
|
|
|
$name = $this->Title;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Fix illegal characters
|
2012-04-04 16:59:30 +02:00
|
|
|
$filter = FileNameFilter::create();
|
2011-08-26 17:57:05 +02:00
|
|
|
$name = $filter->filter($name);
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We might have just turned it blank, so check again.
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!$name) {
|
|
|
|
$name = 'new-folder';
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// If it's changed, check for duplicates
|
|
|
|
if($oldName && $oldName != $name) {
|
2015-05-19 07:43:00 +02:00
|
|
|
$base = pathinfo($name, PATHINFO_FILENAME);
|
2010-10-15 05:15:38 +02:00
|
|
|
$ext = self::get_file_extension($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix = 1;
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2014-01-12 08:54:55 +01:00
|
|
|
while(File::get()->filter(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Name' => $name,
|
2014-01-12 08:54:55 +01:00
|
|
|
'ParentID' => (int) $this->ParentID
|
|
|
|
))->exclude(array(
|
|
|
|
'ID' => $this->ID
|
|
|
|
))->first()
|
|
|
|
) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix++;
|
2015-05-19 07:43:00 +02:00
|
|
|
$name = "$base-$suffix.$ext";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Update actual field value
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Name', $name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-02 04:41:10 +02:00
|
|
|
// Update title
|
|
|
|
if(!$this->Title) {
|
|
|
|
$this->Title = str_replace(array('-','_'),' ', preg_replace('/\.[^.]+$/', '', $name));
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Gets the URL of this file
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-18 00:50:18 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAbsoluteURL() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$url = $this->getURL();
|
|
|
|
if($url) {
|
|
|
|
return Director::absoluteURL($url);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2008-03-18 00:50:18 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-18 00:50:18 +01:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Gets the URL of this file
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-04-12 05:29:56 +02:00
|
|
|
* @uses Director::baseURL()
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param bool $grant Ensures that the url for any protected assets is granted for the current user.
|
2008-03-18 00:50:18 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
public function getURL($grant = true) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if($this->File->exists()) {
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->File->getURL($grant);
|
2016-04-28 01:41:40 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-13 01:02:10 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Get URL, but without resampling.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param bool $grant Ensures that the url for any protected assets is granted for the current user.
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return string
|
2010-10-13 01:02:10 +02:00
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
public function getSourceURL($grant = true) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if($this->File->exists()) {
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->File->getSourceURL($grant);
|
2016-04-28 01:41:40 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* @todo Coupling with cms module, remove this method.
|
2015-09-15 04:52:02 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2010-10-15 05:12:39 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function DeleteLink() {
|
2016-07-19 11:58:35 +02:00
|
|
|
return Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
AdminRootController::admin_url(),
|
|
|
|
"assets/removefile/",
|
|
|
|
$this->ID
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
|
|
|
* Get expected value of Filename tuple value. Will be used to trigger
|
|
|
|
* a file move on draft stage.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateFilename() {
|
2015-09-15 04:52:02 +02:00
|
|
|
// Check if this file is nested within a folder
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if($parent && $parent->exists()) {
|
|
|
|
return $this->join_paths($parent->getFilename(), $this->Name);
|
2008-06-25 07:52:58 +02:00
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return $this->Name;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* Ensure that parent folders are published before this one is published
|
|
|
|
*
|
|
|
|
* @todo Solve this via triggered publishing / ownership in the future
|
|
|
|
*/
|
|
|
|
public function onBeforePublish() {
|
|
|
|
// Relies on Parent() returning the stage record
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if($parent && $parent->exists()) {
|
2016-04-01 05:27:59 +02:00
|
|
|
$parent->publishRecursive();
|
2016-01-26 04:56:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Update the ParentID and Name for the given filename.
|
|
|
|
*
|
|
|
|
* On save, the underlying DBFile record will move the underlying file to this location.
|
|
|
|
* Thus it will not update the underlying Filename value until this is done.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return $this
|
2010-10-15 05:20:47 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setFilename($filename) {
|
|
|
|
// Check existing folder path
|
|
|
|
$folder = '';
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if($parent && $parent->exists()) {
|
|
|
|
$folder = $parent->Filename;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Detect change in foldername
|
|
|
|
$newFolder = ltrim(dirname(trim($filename, '/')), '.');
|
|
|
|
if($folder !== $newFolder) {
|
|
|
|
if(!$newFolder) {
|
|
|
|
$this->ParentID = 0;
|
2008-06-25 07:52:58 +02:00
|
|
|
} else {
|
2015-09-15 04:52:02 +02:00
|
|
|
$parent = Folder::find_or_make($newFolder);
|
|
|
|
$this->ParentID = $parent->ID;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Update base name
|
|
|
|
$this->Name = basename($filename);
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the file extension
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtension() {
|
2015-09-15 04:52:02 +02:00
|
|
|
return self::get_file_extension($this->Name);
|
2008-04-09 13:08:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:08:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the extension of a filepath or filename,
|
|
|
|
* by stripping away everything before the last "dot".
|
2010-10-15 05:14:59 +02:00
|
|
|
* Caution: Only returns the last extension in "double-barrelled"
|
|
|
|
* extensions (e.g. "gz" for "tar.gz").
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:14:59 +02:00
|
|
|
* Examples:
|
|
|
|
* - "myfile" returns ""
|
|
|
|
* - "myfile.txt" returns "txt"
|
|
|
|
* - "myfile.tar.gz" returns "gz"
|
2008-04-09 13:08:58 +02:00
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_file_extension($filename) {
|
2010-10-15 05:14:59 +02:00
|
|
|
return pathinfo($filename, PATHINFO_EXTENSION);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Given an extension, determine the icon that should be used
|
|
|
|
*
|
|
|
|
* @param string $extension
|
|
|
|
* @return string Icon filename relative to base url
|
|
|
|
*/
|
|
|
|
public static function get_icon_for_extension($extension) {
|
|
|
|
$extension = strtolower($extension);
|
|
|
|
|
|
|
|
// Check if exact extension has an icon
|
2016-04-29 07:50:55 +02:00
|
|
|
if(!file_exists(FRAMEWORK_PATH ."/client/dist/images/app_icons/{$extension}_32.png")) {
|
2015-09-15 04:52:02 +02:00
|
|
|
$extension = static::get_app_category($extension);
|
|
|
|
|
|
|
|
// Fallback to category specific icon
|
2016-04-29 07:50:55 +02:00
|
|
|
if(!file_exists(FRAMEWORK_PATH ."/client/dist/images/app_icons/{$extension}_32.png")) {
|
2015-09-15 04:52:02 +02:00
|
|
|
$extension ="generic";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:50:55 +02:00
|
|
|
return FRAMEWORK_DIR ."/client/dist/images/app_icons/{$extension}_32.png";
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
2008-10-16 05:55:33 +02:00
|
|
|
/**
|
|
|
|
* Return the type of file for the given extension
|
|
|
|
* on the current file name.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFileType() {
|
2015-09-15 04:52:02 +02:00
|
|
|
return self::get_file_type($this->getFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get descriptive type of file based on filename
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return string Description of file
|
|
|
|
*/
|
|
|
|
public static function get_file_type($filename) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$types = array(
|
2012-08-27 00:02:52 +02:00
|
|
|
'gif' => _t('File.GifType', 'GIF image - good for diagrams'),
|
|
|
|
'jpg' => _t('File.JpgType', 'JPEG image - good for photos'),
|
|
|
|
'jpeg' => _t('File.JpgType', 'JPEG image - good for photos'),
|
|
|
|
'png' => _t('File.PngType', 'PNG image - good general-purpose format'),
|
|
|
|
'ico' => _t('File.IcoType', 'Icon image'),
|
|
|
|
'tiff' => _t('File.TiffType', 'Tagged image format'),
|
|
|
|
'doc' => _t('File.DocType', 'Word document'),
|
|
|
|
'xls' => _t('File.XlsType', 'Excel spreadsheet'),
|
|
|
|
'zip' => _t('File.ZipType', 'ZIP compressed file'),
|
|
|
|
'gz' => _t('File.GzType', 'GZIP compressed file'),
|
|
|
|
'dmg' => _t('File.DmgType', 'Apple disk image'),
|
|
|
|
'pdf' => _t('File.PdfType', 'Adobe Acrobat PDF file'),
|
|
|
|
'mp3' => _t('File.Mp3Type', 'MP3 audio file'),
|
|
|
|
'wav' => _t('File.WavType', 'WAV audo file'),
|
|
|
|
'avi' => _t('File.AviType', 'AVI video file'),
|
|
|
|
'mpg' => _t('File.MpgType', 'MPEG video file'),
|
|
|
|
'mpeg' => _t('File.MpgType', 'MPEG video file'),
|
|
|
|
'js' => _t('File.JsType', 'Javascript file'),
|
|
|
|
'css' => _t('File.CssType', 'CSS file'),
|
|
|
|
'html' => _t('File.HtmlType', 'HTML file'),
|
2015-03-10 11:24:03 +01:00
|
|
|
'htm' => _t('File.HtmlType', 'HTML file')
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Get extension
|
|
|
|
$extension = strtolower(self::get_file_extension($filename));
|
|
|
|
return isset($types[$extension]) ? $types[$extension] : 'unknown';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the size of the file type in an appropriate format.
|
2015-09-15 04:52:02 +02:00
|
|
|
*
|
|
|
|
* @return string|false String value, or false if doesn't exist
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getSize() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$size = $this->getAbsoluteSize();
|
2015-09-15 04:52:02 +02:00
|
|
|
if($size) {
|
|
|
|
return static::format_size($size);
|
2016-04-29 07:50:55 +02:00
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return false;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Formats a file size (eg: (int)42 becomes string '42 bytes')
|
2015-09-15 04:52:02 +02:00
|
|
|
*
|
|
|
|
* @todo unit tests
|
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @param int $size
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
public static function format_size($size) {
|
2015-09-15 04:52:02 +02:00
|
|
|
if($size < 1024) {
|
|
|
|
return $size . ' bytes';
|
|
|
|
}
|
|
|
|
if($size < 1024*10) {
|
|
|
|
return (round($size/1024*10)/10). ' KB';
|
|
|
|
}
|
|
|
|
if($size < 1024*1024) {
|
|
|
|
return round($size/1024) . ' KB';
|
|
|
|
}
|
|
|
|
if($size < 1024*1024*10) {
|
|
|
|
return (round(($size/1024)/1024*10)/10) . ' MB';
|
|
|
|
}
|
|
|
|
if($size < 1024*1024*1024) {
|
|
|
|
return round(($size/1024)/1024) . ' MB';
|
|
|
|
}
|
2008-08-25 03:42:27 +02:00
|
|
|
return round($size/(1024*1024*1024)*10)/10 . ' GB';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Convert a php.ini value (eg: 512M) to bytes
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @todo unit tests
|
|
|
|
*
|
|
|
|
* @param string $iniValue
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public static function ini2bytes($iniValue) {
|
|
|
|
switch(strtolower(substr(trim($iniValue), -1))) {
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'g':
|
2015-09-15 04:52:02 +02:00
|
|
|
$iniValue *= 1024;
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'm':
|
2015-09-15 04:52:02 +02:00
|
|
|
$iniValue *= 1024;
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'k':
|
2015-09-15 04:52:02 +02:00
|
|
|
$iniValue *= 1024;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return $iniValue;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* Return file size in bytes.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2008-12-04 23:38:32 +01:00
|
|
|
* @return int
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAbsoluteSize(){
|
2015-09-15 04:52:02 +02:00
|
|
|
return $this->File->getAbsoluteSize();
|
2010-04-12 04:34:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-17 05:51:30 +02:00
|
|
|
public function validate() {
|
2016-09-08 08:07:34 +02:00
|
|
|
$result = ValidationResult::create();
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->File->validate($result, $this->Name);
|
2013-12-16 17:07:33 +01:00
|
|
|
$this->extend('validate', $result);
|
|
|
|
return $result;
|
2010-04-12 04:34:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
/**
|
|
|
|
* Maps a {@link File} subclass to a specific extension.
|
|
|
|
* By default, files with common image extensions will be created
|
2014-08-15 08:53:05 +02:00
|
|
|
* as {@link Image} instead of {@link File} when using
|
2011-08-22 13:04:15 +02:00
|
|
|
* {@link Folder::constructChild}, {@link Folder::addUploadToFolder}),
|
|
|
|
* and the {@link Upload} class (either directly or through {@link FileField}).
|
|
|
|
* For manually instanciated files please use this mapping getter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* Caution: Changes to mapping doesn't apply to existing file records in the database.
|
|
|
|
* Also doesn't hook into {@link Object::getCustomClass()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* @param String File extension, without dot prefix. Use an asterisk ('*')
|
|
|
|
* to specify a generic fallback if no mapping is found for an extension.
|
|
|
|
* @return String Classname for a subclass of {@link File}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_class_for_file_extension($ext) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$map = array_change_key_case(self::config()->class_for_file_extension, CASE_LOWER);
|
2011-08-22 13:04:15 +02:00
|
|
|
return (array_key_exists(strtolower($ext), $map)) ? $map[strtolower($ext)] : $map['*'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
/**
|
|
|
|
* See {@link get_class_for_file_extension()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* @param String|array
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_class_for_file_extension($exts, $class) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!is_array($exts)) {
|
|
|
|
$exts = array($exts);
|
|
|
|
}
|
2011-08-22 13:04:15 +02:00
|
|
|
foreach($exts as $ext) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!is_subclass_of($class, 'SilverStripe\\Assets\\File')) {
|
2011-08-22 13:04:15 +02:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
sprintf('Class "%s" (for extension "%s") is not a valid subclass of File', $class, $ext)
|
|
|
|
);
|
|
|
|
}
|
2013-03-21 19:48:54 +01:00
|
|
|
self::config()->class_for_file_extension = array($ext => $class);
|
2011-08-22 13:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getMetaData() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$this->File->exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return $this->File->getMetaData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMimeType() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->File->exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
return $this->File->getMimeType();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStream() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->File->exists()) {
|
|
|
|
return null;
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this->File->getStream();
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getString() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->File->exists()) {
|
|
|
|
return null;
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this->File->getString();
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) {
|
|
|
|
$result = $this->File->setFromLocalFile($path, $filename, $hash, $variant, $config);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Update File record to name of the uploaded asset
|
|
|
|
if($result) {
|
|
|
|
$this->setFilename($result['Filename']);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) {
|
|
|
|
$result = $this->File->setFromStream($stream, $filename, $hash, $variant, $config);
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Update File record to name of the uploaded asset
|
|
|
|
if($result) {
|
|
|
|
$this->setFilename($result['Filename']);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) {
|
|
|
|
$result = $this->File->setFromString($data, $filename, $hash, $variant, $config);
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Update File record to name of the uploaded asset
|
|
|
|
if($result) {
|
|
|
|
$this->setFilename($result['Filename']);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIsImage() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
public function getFilename() {
|
|
|
|
return $this->File->Filename;
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getHash() {
|
|
|
|
return $this->File->Hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVariant() {
|
|
|
|
return $this->File->Variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a html5 tag of the appropriate for this file (normally img or a)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function forTemplate() {
|
|
|
|
return $this->getTag() ?: '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a html5 tag of the appropriate for this file (normally img or a)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTag() {
|
|
|
|
$template = $this->File->getFrontendTemplate();
|
|
|
|
if(empty($template)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return (string)$this->renderWith($template);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDefaultRecords() {
|
|
|
|
parent::requireDefaultRecords();
|
|
|
|
|
|
|
|
// Check if old file records should be migrated
|
|
|
|
if(!$this->config()->migrate_legacy_file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$migrated = FileMigrationHelper::singleton()->run();
|
|
|
|
if($migrated) {
|
|
|
|
DB::alteration_message("{$migrated} File DataObjects upgraded","changed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Joins one or more segments together to build a Filename identifier.
|
|
|
|
*
|
|
|
|
* Note that the result will not have a leading slash, and should not be used
|
|
|
|
* with local file paths.
|
|
|
|
*
|
|
|
|
* @param string $part,... Parts
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public static function join_paths($part = null) {
|
2015-09-15 04:52:02 +02:00
|
|
|
$args = func_get_args();
|
|
|
|
if(count($args) === 1 && is_array($args[0])) {
|
|
|
|
$args = $args[0];
|
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$parts = array();
|
|
|
|
foreach($args as $arg) {
|
|
|
|
$part = trim($arg, ' \\/');
|
|
|
|
if($part) {
|
|
|
|
$parts[] = $part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('/', $parts);
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function deleteFile() {
|
|
|
|
return $this->File->deleteFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVisibility() {
|
|
|
|
return $this->File->getVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function publishFile() {
|
|
|
|
$this->File->publishFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function protectFile() {
|
|
|
|
$this->File->protectFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function grantFile() {
|
|
|
|
$this->File->grantFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function revokeFile() {
|
|
|
|
$this->File->revokeFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canViewFile() {
|
|
|
|
return $this->File->canViewFile();
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
|
|
|
public function CMSEditLink() {
|
|
|
|
$link = null;
|
|
|
|
$this->extend('updateCMSEditLink', $link);
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function PreviewLink($action = null) {
|
2016-09-19 03:02:26 +02:00
|
|
|
// Since AbsoluteURL can whitelist protected assets,
|
|
|
|
// do permission check first
|
|
|
|
if (!$this->canView()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$link = $this->getIcon();
|
2016-04-27 04:26:39 +02:00
|
|
|
$this->extend('updatePreviewLink', $link, $action);
|
|
|
|
return $link;
|
|
|
|
}
|
2011-08-22 13:04:15 +02:00
|
|
|
}
|