diff --git a/docs/en/02_Developer_Guides/14_Files/01_File_Management.md b/docs/en/02_Developer_Guides/14_Files/01_File_Management.md index 2ec92a276..460d09a34 100644 --- a/docs/en/02_Developer_Guides/14_Files/01_File_Management.md +++ b/docs/en/02_Developer_Guides/14_Files/01_File_Management.md @@ -1,90 +1,98 @@ +title: File management summary: Learn how to work with File and Image records -# File Management +# File management -## Storage via database columns +## Asset admin -Asset storage is provided out of the box via a [Flysystem](http://flysystem.thephpleague.com/) backed store. -However, any class that implements the `AssetStore` interface could be substituted to provide storage backends -via other mechanisms. +Management of files within the CMS is provided via the [silverstripe/asset-admin](https://github.com/silverstripe/silverstripe-asset-admin) +module. This is a rich and user friendly interface supporting most basic file operations, as well as +control over the publishing and security of files. -Internally, files are stored as `[DBFile](api:SilverStripe\Assets\Storage\DBFile)` records on the rows of parent objects. These records are composite fields -which contain sufficient information useful to the configured asset backend in order to store, manage, and -publish files. By default this composite field behind this field stores the following details: +![asset admin](_images/asset-admin-demo.png) +## UploadField -| Field name | Description | -| ---------- | ----------- -| `Hash` | The sha1 of the file content, useful for versioning (if supported by the backend) | -| `Filename` | The internal identifier for this file, which may contain a directory path (not including assets). Multiple versions of the same file will have the same filename. | -| `Variant` | The variant for this file. If a file has multiple derived versions (such as resized files or reformatted documents) then you can point to one of the variants here. | +If you have the [silverstripe/asset-admin](https://github.com/silverstripe/silverstripe-asset-admin) +module installed then this provides a powerful component [api:SilverStripe\AssetAdmin\Forms\UploadField]. +![upload field](_images/upload-field.png) -Note that the `Hash` and `Filename` always point to the original file, if a `Variant` is specified. It is up to the -storage backend to determine how variants are managed. +You can add it to a page as below: -Note that the storage backend used will not be automatically synchronised with the database. Only files which -are loaded into the backend through the asset API will be available for use within a site. - -## Compatibility with 3.x filename paths - -If upgrading to 4.0 from earlier versions when using the default asset store, it's important to consider -how existing files will be migrated. - -Because the filesystem now uses the sha1 of file contents in order to version multiple versions under the same -filename, the default storage paths in 4.0 will not be the same as in 3. - -In order to retain existing file paths in line with framework version 3 you should set the -`\SilverStripe\Assets\Flysystem\FlysystemAssetStore.legacy_filenames` config to true. -Note that this will not allow you to utilise certain file versioning features in 4.0. - - -```yaml - - \SilverStripe\Assets\Flysystem\FlysystemAssetStore: - legacy_filenames: true -``` - -## Loading content into `DBFile` - -A file can be written to the backend from a file which exists on the local filesystem (but not necessarily -within the assets folder). - -For example, to load a temporary file into a DataObject you could use the below: ```php - use SilverStripe\ORM\DataObject; + Image::class, + ]; + + public function getCMSFields() { - private static $db = [ - 'Image' => 'DBFile' - ]; + $fields = parent::getCMSFields(); + $fields->addFieldToTab('Root.Main', UploadField::create('Banner', 'Page Banner'), 'Content'); + return $fields; } - - // Image could be assigned in other parts of the code using the below - $banner = new Banner(); - $banner->Image->setFromLocalFile($tempfile['path'], 'uploads/banner-file.jpg'); - +} ``` -When uploading a file it's normally necessary to give the file a useful name and directory, otherwise the -asset storage backend will choose one for you. +UploadField options include: -Alternatively, content can be loaded using one of the below methods: + - setIsMultiUpload() - Set to allow many files per field, or one only. + - setAllowedExtensions() - Set list of extensionse this field can accept. + - setAllowedFileCategories() - Alternatively specify allowed extensions via category instead. + - setFolderName() - Name of folder to upload into + - getValidator() - Get instance of validator to specify custom validation rules + +## File visibility + +In order to ensure that assets are made public you should check the following: + + - The "Who can view this file?" option is set to "Anyone" or "Inherit" in the asset-admin. This can be checked + via `File::canView()` or `File::$CanViewType` property. + - The file is published, or is owned by a published record. This can be checked via `File::isPublished()` + - The file exists on disk, and has not been removed. This can be checked by `File::exists()` + +## File shortcodes + +Shortcodes represent an embeded asset within a block of HTML text. For instance, this is the content +of a page with a shortcode image: + +```html +
Welcome to SilverStripe! This is the default homepage.
+[image src="/assets/e43fb87dc0/12824172.jpeg" id="27" width="400" height="400" class="leftAlone ss-htmleditorfield-file image" title="My Image"]
+``` + +File shortcodes have the following properties: + + - canView() will not be checked for the file itself: Instead this will be inherited from the parent record + this is embedded within. + - The file is automatically "owned", meaning that publishing the page will also publish the embedded file. + +Within the CMS shortcodes can be added via either the "Insert Media" modal, or the "Link to a file" +buttons provided via the [silverstripe/asset-admin](https://github.com/silverstripe/silverstripe-asset-admin) +module. + +## Creating files in PHP + +When working with files in PHP you can upload a file into a [api:SilverStripe\Assets\File] dataobject +using one of the below methods: | Method | Description | | -------------------------- | --------------------------------------- | -| `DBFile::setFromLocalFile` | Load a local file into the asset store | -| `DBFile::setFromStream` | Will store content from a stream | -| `DBFile::setFromString` | Will store content from a binary string | +| `File::setFromLocalFile` | Load a local file into the asset store | +| `File::setFromStream` | Will store content from a stream | +| `File::setFromString` | Will store content from a binary string | -The result of these methods will be an array of data (tuple) which contains the values of the above fields -(Filename, Hash, and Variant). - -## Conflict resolution +### Upload conflict resolution When storing files, it's possible to determine the mechanism the backend should use when it encounters an existing file pattern. The conflict resolution to use can be passed into the third parameter of the @@ -97,69 +105,98 @@ above methods (after content and filename). The available constants are: | `AssetStore::CONFLICT_RENAME` | The backend will choose a new name. | | `AssetStore::CONFLICT_USE_EXISTING` | The existing file will be used | - If no conflict resolution scheme is chosen, or an unsupported one is requested, then the backend will choose one. The default asset store supports each of these. -## Getting content from a `DBFile` - -When placed into a template (e.g. `$MyFileField`) then `[DBFile](api:SilverStripe\Assets\Storage\DBFile)` will automatically generate the appropriate -template for the file mime type. For images, this will embed an image tag. For documents a download link will be presented. +## Accessing files via PHP As with storage, there are also different ways of loading the content (or properties) of the file: | Method | Description | | ------------------------ | ---------------------------------------------------------- | -| `DBFile::getStream` | Will get an output stream of the file content | -| `DBFile::getString` | Gets the binary content | -| `DBFile::getURL` | Gets the url for this resource. May or may not be absolute | -| `DBFile::getAbsoluteURL` | Gets the absolute URL to this resource | -| `DBFile::getMimeType` | Get the mime type of this file | -| `DBFile::getMetaData` | Gets other metadata from the file as an array | +| `File::getStream` | Will get an output stream of the file content | +| `File::getString` | Gets the binary content | +| `File::getURL` | Gets the url for this resource. May or may not be absolute | +| `File::getAbsoluteURL` | Gets the absolute URL to this resource | +| `File::getMimeType` | Get the mime type of this file | +| `File::getMetaData` | Gets other metadata from the file as an array | + +## Modifying files + +In order to move or rename a file you can simply update the Name property, or assign the ParentID to a new +folder. Please note that these modifications are made simply on the draft stage, and will not be copied +to live until a publish is made (either on this object, or cascading from a parent). + +When files are renamed using the ORM, all file variants are automatically renamed at the same time. + +```php +$file = File::get()->filter('Name', 'oldname.jpg')->first(); +if ($file) { + // The below will move 'oldname.jpg' and 'oldname__variant.jpg' + // to 'newname.jpg' and 'newname__variant.jpg' respectively + $file->Name = 'newname.jpg'; + $file->write(); +} +``` + +## File versioning + +File versioning is extended with the [silverstripe/versioned](https://github.com/silverstripe/silverstripe-versioned/) +module, which provides not only a separate draft and live stages for any file, but also allows a complete file +history of modifications to be tracked. + +To support this feature the [api:SilverStripe\Assets\AssetControlExtension] provides support for tracking +references to physical files, ensuring published assets are accessible, protecting non-published assets, +and archiving / deleting assets after the final reference has been deleted. + +### Configuring file ownership + +When working with files attached to other versioned dataobjects it is necessary to configure ownership +of these assets from the parent record. This ensures that whenever a Page (or other record type) +is published, all assets that are used by this record are published with it. + +For example: + +```php + Image::class, + ]; + private static $owns = ['Banner']; +} +``` + +### Avoid exclusive relationships + +Due to the shared nature of assets, it is not recommended to assign any 1-to-many (or exclusive 1-to-1) relationship +between any objects and a File. E.g. a Page has_many File, or Page belongs_to File. -As with other db field types, `DBField` can also be subclassed or extended to add additional properties -(such as SEO related fields). +Instead it is recommended to use either a Page has_one File for many-to-1 (or 1-to-1) relationships, or +Page many_many File for many-to-many relationships. -## Storage via `File` DataObject +### Unpublishing assets -Other than files stored exclusively via DBFile, files can also exist as subclasses of the `File` DataObject. +Assets can be unpublished on a case by case basis via the asset admin section. Please note that +when unpublishing an asset, this will remove this asset from all live pages which currently link to +or embed those images. -Each record has the following database fields: +### Configuring file archiving -| Field name | Description | -| ---------- | ----------- | -| `ClassName` | The class name of the file (e.g. File, Image or Folder). | -| `Name` | The 'basename' of the file, or the folder name. For example 'my-image.jpg', or 'images' for a folder. | -| `Title` | The optional, human-readable title of the file for display only (doesn't apply to folders). | -| `File` | The `[DBFile](api:SilverStripe\Assets\Storage\DBFile)` field (see above) which stores the underlying asset content. | -| `ShowInSearch` | Whether the file should be shown in search results, defaults to '1'. See ["Tutorial 4 - Site Search"](/tutorials/site_search) for enabling search. | -| `ParentID` | The ID of the parent Folder that this File/Folder is in. A ParentID of '0' indicates that this is a top level record. | -| `OwnerID` | The ID of the Member that 'owns' the File/Folder (not related to filesystem permissions). | +By default files which do not exist on either the live or draft stage (but do have a version history) +are removed from the filesystem. +In order to permanantly keep a record of all past physical files you can set the `File.keep_archived_assets` +config option to true. This will ensure that historic files can always be restored, albeit at a cost to disk +storage. -## Management through the "Files" section of the CMS - -If you have the CMS module installed, you can manage files, folders and images in the "Files" section of the CMS. Inside this section, you will see a list of files and folders like below: - -![](../../_images/assets.png) - -You can click on any file to edit it, or click on any folder to open it. To delete a file or a folder, simply click the red 'X' symbol next to it. If you click to open a folder, you can go back up one level by clicking the 'up' arrow above the folder name (highlighted below): - -![](../../_images/assets_up.png) - -Once you click to edit a file, you will see a form similar to the one below, in which you can edit the file's title, filename, owner, or even change which folder the file is located in: - -![](../../_images/assets_editform.png) - -You may also notice the 'Sync files' button (highlighted below). This button allows CMS users to 'synchronise' the database (remember, all files/folders are stored as database records) with the filesystem. This is particularly useful if someone has uploaded or removed files/folders via FTP, for example. - -![](../../_images/assets_sync.png) - -## Upload - -Files can be managed through forms in one way: - - * [FileField](api:SilverStripe\Forms\FileField): provides a simple HTML input with a type of "file". - -_NOTE_: Modules are also available to handle Files. +```yaml +SilverStripe\Assets\File: + keep_archived_assets: true +``` diff --git a/docs/en/02_Developer_Guides/14_Files/03_File_Security.md b/docs/en/02_Developer_Guides/14_Files/03_File_Security.md index a5795a7f6..e49087457 100644 --- a/docs/en/02_Developer_Guides/14_Files/03_File_Security.md +++ b/docs/en/02_Developer_Guides/14_Files/03_File_Security.md @@ -18,13 +18,11 @@ For instance, in order to write an asset to a protected location you can use the config option: - ```php - $store = singleton(AssetStore::class); - $store->setFromString('My protected content', 'Documents/Mydocument.txt', null, null, [ - 'visibility' => AssetStore::VISIBILITY_PROTECTED - ]); - +$store = singleton(AssetStore::class); +$store->setFromString('My protected content', 'Documents/Mydocument.txt', null, null, [ + 'visibility' => AssetStore::VISIBILITY_PROTECTED +]); ``` ## User access control @@ -40,16 +38,15 @@ control access to embedded assets at a template level. ```ss - -