832 lines
21 KiB
PHP
832 lines
21 KiB
PHP
<?php
|
|
|
|
/**
|
|
* MediaEngine Example Codes - Clean Version
|
|
*
|
|
* This file contains comprehensive examples of how to use the MediaEngine
|
|
* for both URL-based and file-based media handling.
|
|
*
|
|
* Features:
|
|
* - Three modes: 'save_file', 'download', 'save_url'
|
|
* - Full backward compatibility
|
|
* - Auto-generated filenames (optional)
|
|
* - Auto-detected MIME types
|
|
* - Smart mode detection
|
|
*
|
|
* Parameter Order:
|
|
* addMedia(mediaCollectionKey, mediaType, mediaSource, mediaProvider,
|
|
* fileContent, url, mode, fileName, disk, name, userId, mimeType)
|
|
*/
|
|
|
|
namespace App\Helpers\FirstParty\MediaEngine;
|
|
|
|
class MediaEngineExampleCodes
|
|
{
|
|
/**
|
|
* ============================================
|
|
* MODE-BASED EXAMPLES
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Basic examples showing all three modes
|
|
*/
|
|
public function basicModeExamples()
|
|
{
|
|
$userId = 123;
|
|
|
|
// MODE: 'save_file' (default) - Store file content directly
|
|
$fileContent = file_get_contents('/path/to/image.jpg');
|
|
$saveFileExample = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent, // fileContent provided
|
|
null, // url null
|
|
'save_file' // mode: save file content to disk
|
|
);
|
|
|
|
// MODE: 'download' - Download from URL and store locally
|
|
$downloadExample = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null, // fileContent null
|
|
'https://example.com/image.jpg', // url provided
|
|
'download' // mode: download URL and store to disk
|
|
);
|
|
|
|
// MODE: 'save_url' - Just save URL reference (no download)
|
|
$saveUrlExample = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null, // fileContent null
|
|
'https://example.com/image.jpg', // url provided
|
|
'save_url' // mode: just save URL reference
|
|
);
|
|
|
|
return [$saveFileExample, $downloadExample, $saveUrlExample];
|
|
}
|
|
|
|
/**
|
|
* Practical scenarios for each mode
|
|
*/
|
|
public function practicalModeScenarios()
|
|
{
|
|
$userId = 456;
|
|
|
|
// SAVE_URL: Fast external references (CDNs, social media)
|
|
$cdnReference = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://cdn.example.com/fast-image.jpg',
|
|
'save_url'
|
|
);
|
|
|
|
// DOWNLOAD: Critical content that needs local backup
|
|
$criticalDownload = MediaEngine::addMedia(
|
|
'user-i',
|
|
'document',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://external-site.com/important-doc.pdf',
|
|
'download'
|
|
);
|
|
|
|
// SAVE_FILE: Direct user uploads
|
|
$directUpload = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
file_get_contents($_FILES['upload']['tmp_name'])
|
|
);
|
|
|
|
// AI content that might expire - download for safety
|
|
$aiContentBackup = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'ai_generated',
|
|
'runway_ml',
|
|
null,
|
|
'https://temp-ai-api.com/video_123.mp4',
|
|
'download'
|
|
);
|
|
|
|
return [$cdnReference, $criticalDownload, $directUpload, $aiContentBackup];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* MINIMAL EXAMPLES
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Absolute minimal calls with smart mode detection
|
|
*/
|
|
public function minimalExamples()
|
|
{
|
|
// URL-based - auto-detects to 'save_url'
|
|
$urlMinimal = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg'
|
|
);
|
|
|
|
// File-based - uses default 'save_file'
|
|
$fileContent = file_get_contents('/path/to/file.jpg');
|
|
$fileMinimal = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent
|
|
);
|
|
|
|
// Download - explicit mode required
|
|
$downloadMinimal = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg',
|
|
'download'
|
|
);
|
|
|
|
return [$urlMinimal, $fileMinimal, $downloadMinimal];
|
|
}
|
|
|
|
/**
|
|
* Clean one-liners for common use cases
|
|
*/
|
|
public function oneLineExamples()
|
|
{
|
|
$userId = 999;
|
|
|
|
// Social media import
|
|
$social = MediaEngine::addMedia('user-i', 'image', 'user_uploaded', 'social', null, 'https://instagram.com/photo.jpg');
|
|
|
|
// AI content generation
|
|
$ai = MediaEngine::addMedia('user-v', 'video', 'ai_generated', 'runway', null, 'https://ai-api.com/video.mp4', 'download');
|
|
|
|
// File upload
|
|
$upload = MediaEngine::addMedia('user-a', 'audio', 'user_uploaded', 'web_app', file_get_contents('/tmp/audio.mp3'));
|
|
|
|
// Background music
|
|
$bgm = MediaEngine::addMedia('user-a', 'audio', 'system', 'library', null, 'https://music.com/track.mp3');
|
|
|
|
return [$social, $ai, $upload, $bgm];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* URL-BASED EXAMPLES
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* URL examples with save_url mode (reference only)
|
|
*/
|
|
public function urlReferenceExamples()
|
|
{
|
|
// Basic URL reference
|
|
$imageRef = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/sunset.jpg',
|
|
'save_url'
|
|
);
|
|
|
|
// Video reference with user
|
|
$videoRef = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/vacation.mp4',
|
|
'save_url',
|
|
null,
|
|
'r2',
|
|
null,
|
|
123
|
|
);
|
|
|
|
// Audio reference
|
|
$audioRef = MediaEngine::addMedia(
|
|
'user-a',
|
|
'audio',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/song.mp3',
|
|
'save_url'
|
|
);
|
|
|
|
return [$imageRef, $videoRef, $audioRef];
|
|
}
|
|
|
|
/**
|
|
* URL examples with download mode (download and store)
|
|
*/
|
|
public function urlDownloadExamples()
|
|
{
|
|
// Download image and store
|
|
$downloadedImage = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/important.jpg',
|
|
'download'
|
|
);
|
|
|
|
// Download with custom settings
|
|
$customDownload = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/avatar.jpg',
|
|
'download',
|
|
'user-avatar.jpg',
|
|
'r2',
|
|
'User Avatar',
|
|
456,
|
|
'image/jpeg'
|
|
);
|
|
|
|
return [$downloadedImage, $customDownload];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* FILE-BASED EXAMPLES
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* File-based examples with auto-generated names
|
|
*/
|
|
public function fileBasedExamples()
|
|
{
|
|
// Basic file upload with auto-generated name
|
|
$fileContent = file_get_contents('/path/to/image.jpg');
|
|
$autoFile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent
|
|
);
|
|
|
|
// File with explicit mode
|
|
$explicitFile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent,
|
|
null,
|
|
'save_file'
|
|
);
|
|
|
|
// File with custom settings
|
|
$customFile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'document',
|
|
'user_uploaded',
|
|
'web_app',
|
|
file_get_contents('/path/to/doc.pdf'),
|
|
null,
|
|
'save_file',
|
|
'important-doc.pdf',
|
|
'r2',
|
|
'Important Document',
|
|
789
|
|
);
|
|
|
|
return [$autoFile, $explicitFile, $customFile];
|
|
}
|
|
|
|
/**
|
|
* Laravel file upload handling
|
|
*/
|
|
public function laravelFileUploadExamples()
|
|
{
|
|
$userId = 101;
|
|
|
|
// Basic Laravel upload with original filename
|
|
$uploadedFile = request()->file('media');
|
|
if ($uploadedFile) {
|
|
$fileContent = file_get_contents($uploadedFile->getPathname());
|
|
$originalName = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent,
|
|
null,
|
|
'save_file',
|
|
$uploadedFile->getClientOriginalName(),
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
}
|
|
|
|
// Laravel upload with auto-generated filename
|
|
$uploadedFile2 = request()->file('another_media');
|
|
if ($uploadedFile2) {
|
|
$fileContent = file_get_contents($uploadedFile2->getPathname());
|
|
$autoName = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
$fileContent,
|
|
null,
|
|
'save_file',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
}
|
|
|
|
return [$originalName ?? null, $autoName ?? null];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* REAL-WORLD SCENARIOS
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Social media integration examples
|
|
*/
|
|
public function socialMediaIntegration()
|
|
{
|
|
$userId = 202;
|
|
|
|
// Instagram - save reference (fast)
|
|
$instagram = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'instagram',
|
|
null,
|
|
'https://instagram.com/photos/abc123.jpg',
|
|
'save_url',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
|
|
// YouTube thumbnail - download (reliable)
|
|
$youtube = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'system_uploaded',
|
|
'youtube_api',
|
|
null,
|
|
'https://img.youtube.com/vi/VIDEO_ID/maxresdefault.jpg',
|
|
'download'
|
|
);
|
|
|
|
// TikTok - backward compatible
|
|
$tiktok = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'user_uploaded',
|
|
'tiktok',
|
|
null,
|
|
'https://tiktok.com/videos/dance.mp4'
|
|
);
|
|
|
|
return [$instagram, $youtube, $tiktok];
|
|
}
|
|
|
|
/**
|
|
* AI-generated content examples
|
|
*/
|
|
public function aiContentExamples()
|
|
{
|
|
$userId = 303;
|
|
|
|
// ElevenLabs voice - save reference
|
|
$voice = MediaEngine::addMedia(
|
|
'user-a',
|
|
'audio',
|
|
'ai_generated',
|
|
'elevenlabs',
|
|
null,
|
|
'https://api.elevenlabs.io/voice_sample.mp3'
|
|
);
|
|
|
|
// Runway video - download for backup
|
|
$video = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'ai_generated',
|
|
'runway_ml',
|
|
null,
|
|
'https://api.runwayml.com/video_abc.mp4',
|
|
'download',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
|
|
// Midjourney image - save reference
|
|
$image = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'ai_generated',
|
|
'midjourney',
|
|
null,
|
|
'https://cdn.midjourney.com/artwork_def.png',
|
|
'save_url',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
|
|
return [$voice, $video, $image];
|
|
}
|
|
|
|
/**
|
|
* Enterprise/business scenarios
|
|
*/
|
|
public function enterpriseExamples()
|
|
{
|
|
$userId = 404;
|
|
|
|
// CDN assets - reference only
|
|
$cdnAsset = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'system_uploaded',
|
|
'cdn',
|
|
null,
|
|
'https://cdn.company.com/logo.png',
|
|
'save_url'
|
|
);
|
|
|
|
// Important documents - download and backup
|
|
$document = MediaEngine::addMedia(
|
|
'user-d',
|
|
'document',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://external-api.com/contract.pdf',
|
|
'download',
|
|
'contract-2024.pdf',
|
|
'r2',
|
|
'Business Contract',
|
|
$userId
|
|
);
|
|
|
|
// User profile photo - flexible handling
|
|
$profile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://social-login.com/avatar.jpg',
|
|
'download',
|
|
null,
|
|
'r2',
|
|
'Profile Photo',
|
|
$userId
|
|
);
|
|
|
|
return [$cdnAsset, $document, $profile];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* BACKWARD COMPATIBILITY
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Examples showing backward compatibility with auto-detection
|
|
*/
|
|
public function backwardCompatibilityExamples()
|
|
{
|
|
// OLD CODE - still works with auto-detection
|
|
$oldUrl = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg' // Auto-detects 'save_url'
|
|
);
|
|
|
|
$oldFile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
file_get_contents('/path/to/image.jpg') // Uses default 'save_file'
|
|
);
|
|
|
|
// NEW CODE - explicit modes
|
|
$newUrlRef = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg',
|
|
'save_url'
|
|
);
|
|
|
|
$newDownload = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg',
|
|
'download'
|
|
);
|
|
|
|
$newFile = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
file_get_contents('/path/to/image.jpg'),
|
|
null,
|
|
'save_file'
|
|
);
|
|
|
|
return [$oldUrl, $oldFile, $newUrlRef, $newDownload, $newFile];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* BULK OPERATIONS
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Bulk processing examples
|
|
*/
|
|
public function bulkOperationExamples()
|
|
{
|
|
$userId = 505;
|
|
$urls = [
|
|
'https://example.com/photo1.jpg',
|
|
'https://example.com/photo2.png',
|
|
'https://example.com/photo3.gif',
|
|
];
|
|
|
|
$results = [];
|
|
|
|
// Bulk URL references
|
|
foreach ($urls as $url) {
|
|
$media = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'bulk_import',
|
|
null,
|
|
$url,
|
|
'save_url',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
$results[] = $media;
|
|
}
|
|
|
|
// Bulk downloads with error handling
|
|
$downloadResults = [];
|
|
foreach ($urls as $url) {
|
|
try {
|
|
$media = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'bulk_download',
|
|
null,
|
|
$url,
|
|
'download',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
$downloadResults[] = $media;
|
|
} catch (\Exception $e) {
|
|
$downloadResults[] = ['error' => $e->getMessage(), 'url' => $url];
|
|
}
|
|
}
|
|
|
|
return [$results, $downloadResults];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* ERROR HANDLING
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Comprehensive error handling examples
|
|
*/
|
|
public function errorHandlingExamples()
|
|
{
|
|
try {
|
|
// Invalid URL
|
|
$invalidUrl = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://invalid-domain-12345.com/image.jpg',
|
|
'download'
|
|
);
|
|
} catch (\RuntimeException $e) {
|
|
// Handle download failure
|
|
$error1 = ['error' => 'Download failed: '.$e->getMessage()];
|
|
}
|
|
|
|
try {
|
|
// Invalid collection key
|
|
$invalidCollection = MediaEngine::addMedia(
|
|
'invalid-collection',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/image.jpg'
|
|
);
|
|
} catch (\InvalidArgumentException $e) {
|
|
// Handle invalid collection
|
|
$error2 = ['error' => 'Invalid collection: '.$e->getMessage()];
|
|
}
|
|
|
|
try {
|
|
// Invalid mode
|
|
$invalidMode = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/image.jpg',
|
|
'invalid_mode'
|
|
);
|
|
} catch (\InvalidArgumentException $e) {
|
|
// Handle invalid mode
|
|
$error3 = ['error' => 'Invalid mode: '.$e->getMessage()];
|
|
}
|
|
|
|
return [$error1 ?? null, $error2 ?? null, $error3 ?? null];
|
|
}
|
|
|
|
/**
|
|
* ============================================
|
|
* ADVANCED USE CASES
|
|
* ============================================
|
|
*/
|
|
|
|
/**
|
|
* Advanced scenarios with custom settings
|
|
*/
|
|
public function advancedExamples()
|
|
{
|
|
$userId = 606;
|
|
|
|
// Custom filename and MIME type
|
|
$customized = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://example.com/photo.jpg',
|
|
'download',
|
|
'custom-avatar-2024.jpg',
|
|
'r2',
|
|
'Profile Avatar',
|
|
$userId,
|
|
'image/jpeg'
|
|
);
|
|
|
|
// Multiple disk storage
|
|
$s3Storage = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'user_uploaded',
|
|
'web_app',
|
|
file_get_contents('/path/to/video.mp4'),
|
|
null,
|
|
'save_file',
|
|
'presentation-video.mp4',
|
|
's3',
|
|
'Presentation Video',
|
|
$userId
|
|
);
|
|
|
|
// Complex AI workflow
|
|
$aiWorkflow = MediaEngine::addMedia(
|
|
'user-a',
|
|
'audio',
|
|
'ai_generated',
|
|
'custom_pipeline',
|
|
null,
|
|
'https://ai-pipeline.com/processed-audio.wav',
|
|
'download',
|
|
'processed-voice-'.time().'.wav',
|
|
'r2',
|
|
'AI Processed Voice',
|
|
$userId,
|
|
'audio/wav'
|
|
);
|
|
|
|
return [$customized, $s3Storage, $aiWorkflow];
|
|
}
|
|
|
|
/**
|
|
* Performance optimization examples
|
|
*/
|
|
public function performanceExamples()
|
|
{
|
|
$userId = 707;
|
|
|
|
// Fast CDN reference (no download overhead)
|
|
$fastReference = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://fast-cdn.com/optimized-image.webp',
|
|
'save_url'
|
|
);
|
|
|
|
// Strategic download for critical assets
|
|
$criticalAsset = MediaEngine::addMedia(
|
|
'user-i',
|
|
'image',
|
|
'system_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://external-api.com/logo.png',
|
|
'download',
|
|
'company-logo.png',
|
|
'r2',
|
|
'Company Logo'
|
|
);
|
|
|
|
// Lazy loading approach
|
|
$lazyLoad = MediaEngine::addMedia(
|
|
'user-v',
|
|
'video',
|
|
'user_uploaded',
|
|
'web_app',
|
|
null,
|
|
'https://video-service.com/large-video.mp4',
|
|
'save_url',
|
|
null,
|
|
'r2',
|
|
null,
|
|
$userId
|
|
);
|
|
|
|
return [$fastReference, $criticalAsset, $lazyLoad];
|
|
}
|
|
}
|