refactor: modularize ProjectsController into service-based architecture
- Split monolithic 600+ line controller into 5 focused service classes - AuthorizationService: centralized auth/permission handling - FileSystemService: path management and file operations - MediaAnalysisService: image/video metadata extraction - ProjectService: business logic for project operations - AdsOverviewService: complex recursive tree generation - Apply Single Responsibility Principle for better maintainability - Preserve all existing functionality and API compatibility - Remove temporary backup and development files - Improve code organization and reusability
This commit is contained in:
96
backend/src/Services/FileSystemService.php
Normal file
96
backend/src/Services/FileSystemService.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* FileSystemService
|
||||
* Handles file system operations and path management
|
||||
*/
|
||||
class FileSystemService {
|
||||
|
||||
/**
|
||||
* Determine the correct path to the area folder based on environment
|
||||
*/
|
||||
public static function getAreaPath() {
|
||||
// Check if we're in development structure (backend/src/Api)
|
||||
$devPath = __DIR__ . '/../../../area';
|
||||
if (is_dir($devPath)) {
|
||||
return '/../../../area';
|
||||
}
|
||||
|
||||
// Check if we're in deployment structure (src/Api)
|
||||
$deployPath = __DIR__ . '/../../area';
|
||||
if (is_dir($deployPath)) {
|
||||
return '/../../area';
|
||||
}
|
||||
|
||||
// Fallback to development path
|
||||
return '/../../../area';
|
||||
}
|
||||
|
||||
/**
|
||||
* Central list of files to ignore
|
||||
*/
|
||||
public static function getIgnoredFiles() {
|
||||
return [
|
||||
'.DS_Store', // macOS System file
|
||||
'Thumbs.db', // Windows Thumbnail cache
|
||||
'desktop.ini', // Windows Desktop configuration
|
||||
'.gitignore', // Git configuration
|
||||
'.gitkeep', // Git placeholder
|
||||
'config.yaml', // Configuration files
|
||||
'config.yml',
|
||||
'setup.yaml',
|
||||
'setup.yml',
|
||||
'.htaccess', // Apache configuration
|
||||
'index.php', // PHP index (except in HTML folders)
|
||||
'web.config', // IIS configuration
|
||||
'.env', // Environment variables
|
||||
'.env.local',
|
||||
'README.md', // Documentation
|
||||
'readme.txt',
|
||||
'license.txt',
|
||||
'LICENSE'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file should be ignored
|
||||
*/
|
||||
public static function shouldIgnoreFile($filename) {
|
||||
$ignoredFiles = self::getIgnoredFiles();
|
||||
return in_array(strtolower($filename), array_map('strtolower', $ignoredFiles));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file type based on extension
|
||||
*/
|
||||
public static function getFileType($filename) {
|
||||
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||
|
||||
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'])) {
|
||||
return 'image';
|
||||
} elseif (in_array($ext, ['mp4', 'mov', 'avi', 'webm'])) {
|
||||
return 'video';
|
||||
} elseif (in_array($ext, ['html', 'htm'])) {
|
||||
return 'html';
|
||||
}
|
||||
|
||||
return 'other';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create safe URL for file access
|
||||
*/
|
||||
public static function createFileUrl($clientDir, $projectName, $adsFolders, $filename) {
|
||||
$urlParts = array_map('rawurlencode', $adsFolders);
|
||||
return "/area/" . rawurlencode($clientDir) . "/" . rawurlencode($projectName) . "/ads/"
|
||||
. implode('/', $urlParts) . (count($urlParts) ? '/' : '') . rawurlencode($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate path is within allowed client directory
|
||||
*/
|
||||
public static function validatePath($realPath, $clientDir) {
|
||||
$allowedBasePath = realpath(__DIR__ . self::getAreaPath() . '/' . $clientDir);
|
||||
return $realPath && strpos($realPath, $allowedBasePath) === 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user