This commit is contained in:
ct
2025-06-13 09:44:18 +08:00
parent 8d6e86ebb3
commit 2fd4d42aec
54 changed files with 9917 additions and 184 deletions

View File

@@ -19,6 +19,12 @@ class MediaEngine
public static function getMediaCloudUrl($media)
{
// Check for uploaded_url first (URL-based media)
if (! empty($media->uploaded_url)) {
return $media->uploaded_url;
}
// Fallback to storage disk URL (file-based media)
return Storage::disk($media->disk)->url($media->file_path.$media->file_name);
}
@@ -37,11 +43,31 @@ public static function loadMediaToLocalTemp($uuid)
return $result;
}
$tempPath = 'temp_'.$media->file_name;
// Handle URL-based media
if (! empty($media->uploaded_url)) {
if (! Storage::disk(self::LOCAL_TEMP_DISK)->exists($tempPath)) {
$fileContent = @file_get_contents($media->uploaded_url);
if ($fileContent !== false) {
if (Storage::disk(self::LOCAL_TEMP_DISK)->put($tempPath, $fileContent)) {
$result['temp_path'] = $tempPath;
$result['temp'] = Storage::disk(self::LOCAL_TEMP_DISK)->path($tempPath);
}
}
} else {
$result['temp_path'] = $tempPath;
$result['temp'] = Storage::disk(self::LOCAL_TEMP_DISK)->path($tempPath);
}
return $result;
}
// Handle file-based media (existing behavior)
if (! Storage::disk($media->disk)->exists($media->file_path.$media->file_name)) {
return $result;
}
$tempPath = 'temp_'.$media->file_name;
$fileContent = Storage::disk($media->disk)->get($media->file_path.$media->file_name);
if (! Storage::disk(self::LOCAL_TEMP_DISK)->exists($tempPath)) {
@@ -73,9 +99,31 @@ public static function loadMediasToLocalTemp(array $uuids)
'uuid' => $media->uuid,
'temp_path' => null,
'temp' => null,
'cloud_url' => Storage::disk($media->disk)->url($media->file_path.$media->file_name),
'cloud_url' => self::getMediaCloudUrl($media),
];
// Handle URL-based media
if (! empty($media->uploaded_url)) {
if (Storage::disk(self::LOCAL_TEMP_DISK)->exists($tempPath)) {
// File already exists in temp, just set the paths
$singleResult['temp_path'] = $tempPath;
$singleResult['temp'] = Storage::disk(self::LOCAL_TEMP_DISK)->path($tempPath);
} else {
// File needs to be downloaded to temp
$fileContent = @file_get_contents($media->uploaded_url);
if ($fileContent !== false) {
if (Storage::disk(self::LOCAL_TEMP_DISK)->put($tempPath, $fileContent)) {
$singleResult['temp_path'] = $tempPath;
$singleResult['temp'] = Storage::disk(self::LOCAL_TEMP_DISK)->path($tempPath);
}
}
}
$result[] = $singleResult;
continue;
}
// Handle file-based media (existing behavior)
// Check if file exists in cloud storage
if (! Storage::disk($media->disk)->exists($media->file_path.$media->file_name)) {
$result[] = $singleResult; // Add to results but with null temp paths
@@ -148,11 +196,14 @@ public static function deleteMediasFromLocalTemp(array $uuids)
* 'bgm',
* 'user_upload',
* 'web_app',
* 'background_music.mp3',
* $fileContent,
* $fileContent, // File content or null
* $url, // URL or null
* 'save_file', // Mode: 'save_file'|'download'|'save_url'
* 'background_music.mp3', // Optional - auto-generated if null
* 'r2',
* 'name of file',
* $user_id,
* $mimeType // Optional MIME type
* );
*/
public static function addMedia(
@@ -160,12 +211,14 @@ public static function addMedia(
string $mediaType,
string $mediaSource,
string $mediaProvider,
string $fileName,
string $fileContent,
?string $fileContent = null,
?string $url = null,
?string $mode = 'save_file',
?string $fileName = null,
string $disk = 'r2',
?string $name = null,
?int $userId = null,
?string $mimeType = null
) {
$mediaCollection = MediaCollection::where('key', $mediaCollectionKey)->first();
@@ -175,6 +228,97 @@ public static function addMedia(
$config = config("platform.media.{$mediaCollectionKey}");
// Smart mode detection for backward compatibility
if ($mode === 'save_file') {
// If mode is default but we have URL and no fileContent, switch to save_url for backward compatibility
if (! empty($url) && empty($fileContent)) {
$mode = 'save_url';
}
}
// Validate mode parameter
$validModes = ['save_file', 'download', 'save_url'];
if (! in_array($mode, $validModes)) {
throw new \InvalidArgumentException("Invalid mode '{$mode}'. Valid modes are: ".implode(', ', $validModes));
}
// Validate required parameters based on mode
if ($mode === 'save_file' && empty($fileContent)) {
throw new \InvalidArgumentException("fileContent is required when mode is 'save_file'.");
}
if (($mode === 'download' || $mode === 'save_url') && empty($url)) {
throw new \InvalidArgumentException("url is required when mode is '{$mode}'.");
}
// Auto-generate fileName if not provided
if (empty($fileName)) {
if (! empty($url)) {
// Extract filename from URL
$pathInfo = pathinfo(parse_url($url, PHP_URL_PATH));
$fileName = $pathInfo['basename'] ?? null;
// If no filename in URL, generate one
if (empty($fileName) || $fileName === '/') {
$extension = ! empty($config['extension']) ? $config['extension'] : 'file';
$fileName = $mediaType.'_'.epoch_now_timestamp().'.'.$extension;
}
} else {
// Generate filename for file-based media
$extension = ! empty($config['extension']) ? $config['extension'] : 'file';
$fileName = $mediaType.'_'.epoch_now_timestamp().'.'.$extension;
}
}
// Handle different modes
if ($mode === 'save_url') {
// URL-based media handling - just save URL reference
$adjustedFileName = $config['prefix'].epoch_now_timestamp().'-'.$fileName;
// Auto-detect MIME type from URL if not provided
if (empty($mimeType)) {
$fileDetails = self::getFileDetailsbyUrl($url);
$mimeType = $fileDetails->mimetype;
// Fallback to config mime if detection fails
if (empty($mimeType) || $mimeType === 'application/octet-stream') {
$mimeType = $config['mime'];
}
}
$media = new Media([
'uuid' => Str::uuid(),
'media_collection_id' => $mediaCollection->id,
'user_id' => $userId,
'media_type' => $mediaType,
'media_source' => $mediaSource,
'media_provider' => $mediaProvider,
'mime_type' => $mimeType,
'file_name' => $adjustedFileName,
'file_path' => $config['location'], // Keep for consistency
'disk' => $disk,
'name' => $name,
'uploaded_url' => $url,
]);
$media->save();
return $media;
}
// For 'save_file' and 'download' modes, we need to store file content
if ($mode === 'download') {
// Download content from URL
$fileContent = @file_get_contents($url);
if ($fileContent === false) {
throw new \RuntimeException("Failed to download file from URL: {$url}");
}
}
// File-based media handling (for both 'save_file' and 'download' modes)
if (empty($fileContent)) {
throw new \InvalidArgumentException('fileContent is required for file storage.');
}
// Adjust fileName with prefix, postfix, and ensure extension
$adjustedFileName = $config['prefix'].epoch_now_timestamp().'-'.$fileName;
@@ -189,12 +333,12 @@ public static function addMedia(
}
// Get filetype
$mimeType = null;
$detectedMimeType = null;
$tempFile = tempnam(sys_get_temp_dir(), 'mime_');
try {
file_put_contents($tempFile, $fileContent);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $tempFile);
$detectedMimeType = finfo_file($finfo, $tempFile);
finfo_close($finfo);
} finally {
// This ensures the file is deleted even if an exception occurs
@@ -202,8 +346,8 @@ public static function addMedia(
unlink($tempFile);
}
}
if (is_empty($mimeType)) {
$mimeType = $config['mime'];
if (is_empty($detectedMimeType)) {
$detectedMimeType = $config['mime'];
}
$media = new Media([
@@ -213,11 +357,12 @@ public static function addMedia(
'media_type' => $mediaType,
'media_source' => $mediaSource,
'media_provider' => $mediaProvider,
'mime_type' => $mimeType,
'mime_type' => $detectedMimeType,
'file_name' => $adjustedFileName,
'file_path' => $filePath,
'disk' => $disk,
'name' => $name,
'uploaded_url' => null,
]);
$media->save();