Back to October

Media Module

modules/media/README.md

4.4.05.4 KB
Original Source

Media Module

The Media module provides file and folder management for October CMS. It includes the Media Library API, a backend Media Manager interface, the mediafinder form widget for selecting media in forms, and a Twig filter for generating public URLs. Built on Laravel's Storage facade, the Media Library works with local disks, S3, Azure, or any other storage driver -- with path validation, extension allowlists, SVG sanitization, and caching handled transparently.

Architecture Overview

The module has three layers:

  • MediaLibrary - core API for file operations (list, upload, delete, move, search)
  • MediaManager - the backend widget providing the visual file browser
  • MediaFinder - a form widget for selecting files/images from the media library

Key Services

ServiceClassDescription
media.libraryMedia\Classes\MediaLibraryCore file operations API
media.viewsMedia\Helpers\MediaViewVideo/audio markup processing

MediaLibrary API

The MediaLibrary class provides all file operations. Access via MediaLibrary::instance().

Listing and Searching

php
$library = MediaLibrary::instance();

// List folder contents (sorted by title, filtered to images)
$items = $library->listFolderContents('/', 'title', 'image');

// Search across the entire library
$results = $library->findFiles('vacation');

File Operations

php
// Upload a file
$library->putFile('/photos/pic.jpg', $uploadedFile);

// Write raw contents
$library->put('/docs/readme.txt', $contents);

// Move and rename
$library->moveFile('/old/path.jpg', '/new/path.jpg');
$library->moveFolder('/old/folder', '/new/folder');

// Copy a folder
$library->copyFolder('/source', '/destination');

// Delete
$library->deleteFiles(['/photos/old.jpg', '/photos/temp.jpg']);
$library->deleteFolder('/temp');

URL Generation

php
$url = $library->getPathUrl('/photos/pic.jpg');

MediaLibraryItem

Each item returned by listFolderContents() or findFiles() is a MediaLibraryItem with:

  • $path, $title, $type (file or folder), $size, $lastModified, $publicUrl
  • isFile(), getFileType() (image, video, audio, document)
  • sizeToString(), lastModifiedAsString()

MediaManager Widget

The MediaManager is a persistent backend widget available on all backend pages (if the user has permission). It provides a complete file management experience without requiring any third-party packages or custom integration:

  • Grid, list, and tile view modes
  • File upload with drag-and-drop
  • Search, navigation, sorting, filtering (by type: image, video, audio, document)
  • Create, delete, rename, and move folders and files
  • Image cropping tool
  • Thumbnail generation
  • Duplicate file detection

MediaFinder Form Widget

Select media files in backend forms:

yaml
# Image mode
featured_image:
    type: mediafinder
    mode: image
    maxItems: 1
    imageWidth: 190
    imageHeight: 190

# File mode
download:
    type: mediafinder
    mode: file

# Folder mode
gallery_folder:
    type: mediafinder
    mode: folder

Twig Integration

The module registers the | media filter for generating public URLs to media files:

twig

Works with strings, arrays, collections, and file attachment objects.

Storage

The Media Library uses Laravel's Storage facade with the media disk. By default this maps to storage/app/media but can be configured to use S3, Azure, or any other Laravel storage driver.

Features:

  • Caching - file listings are cached (10-minute TTL by default) and invalidated on changes
  • Path validation - prevents directory traversal attacks
  • Extension allowlist - only configured file types can be uploaded
  • SVG sanitization - scripts are stripped from SVG files
  • Filename normalization - consistent file naming

Configuration

Media settings are in config/media.php:

KeyDescription
item_cache_ttlCache duration in minutes (default: 10)
auto_renameAuto-rename uploads (null or 'slug')
clean_vectorsSanitize SVG files (default: true)
default_extensionsAllowed file extensions
image_extensionsExtensions classified as images
video_extensionsExtensions classified as video
audio_extensionsExtensions classified as audio
ignore_filesFilenames to ignore (e.g. .svn, .git)
ignore_patternsRegex patterns to ignore (e.g. ^\..*)

Extension Points

Events

EventDescription
media.file.beforeUploadBefore a file is uploaded
media.file.uploadAfter a file is uploaded (path can be modified by reference)
media.file.deleteAfter a file is deleted
media.file.renameAfter a file is renamed
media.file.moveAfter a file is moved
media.folder.createAfter a folder is created
media.folder.deleteAfter a folder is deleted
media.folder.renameAfter a folder is renamed
media.folder.moveAfter a folder is moved

Example

php
Event::listen('media.file.upload', function ($manager, &$path, $uploadedFile) {
    // Modify the path or perform post-processing
});

Permissions

PermissionDescription
media.libraryAccess the Media Manager
media.library.createUpload files
media.library.deleteDelete, rename, and move items