Language updates. New upload form. new classes.

This commit is contained in:
Cody Cook 2025-02-22 00:20:39 -08:00
commit 8f3061ab99
62 changed files with 3107 additions and 1883 deletions

View file

@ -4,87 +4,125 @@ namespace DJMixHosting;
class Upload
{
private $file;
private $file_name;
private $file_size;
private $file_tmp;
private $file_type;
private $file_ext;
private $file_path;
private $extensions = array("mp3", "zip");
private $upload_dir = "uploads/";
private $errors = array();
private $ok = false;
private $config;
private $uuid = "";
protected $file;
protected $config;
protected $errors = [];
protected $uploadDir;
protected $uuid;
protected $fileExt;
protected $localPath;
public function __construct($file, $config)
public function __construct(array $file, array $config)
{
$this->file = $file;
$this->file_name = $file['name'];
$this->file_size = $file['size'];
$this->file_tmp = $file['tmp_name'];
$this->file_type = $file['type'];
$this->config = $config;
$ext = explode('.', $file['name']);
$this->file_ext = strtolower(end($ext));
$this->uploadDir = rtrim($config['uploads']['tmp_path'], '/') . '/';
$this->uuid = uniqid();
$this->fileExt = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
}
private function check_file_size(): bool
public function validate(): bool
{
if ($this->file_size > $this->config['uploads']['max_file_size']){
$this->errors[] = "File size is too large";
return false;
if ($this->file['error'] !== UPLOAD_ERR_OK) {
$this->errors[] = "File upload error.";
}
return true;
}
private function check_file_extension(): bool
{
if (!in_array($this->file_ext, $this->extensions)){
$this->errors[] = "Invalid file extension";
return false;
if ($this->file['size'] > $this->config['uploads']['max_file_size']) {
$this->errors[] = "File is too large.";
}
return true;
}
private function check_file_exists(): bool
{
if (file_exists($this->upload_dir . $this->file_name)){
$this->errors[] = "File already exists";
return false;
if (!in_array($this->fileExt, $this->config['uploads']['allowed_file_types'])) {
$this->errors[] = "File type not allowed.";
}
return true;
if (!in_array($this->file['type'], $this->config['uploads']['allowed_mime_type'])) {
$this->errors[] = "MIME type not allowed.";
}
return empty($this->errors);
}
private function move_file(): bool
public function getErrors(): array
{
if (move_uploaded_file($this->file_tmp, $this->upload_dir . $this->uuid)){
$this->file_path = $this->upload_dir . $this->uuid;
return $this->errors;
}
public function moveFile(): bool
{
$destination = $this->uploadDir . $this->uuid . "." . $this->fileExt;
if (move_uploaded_file($this->file['tmp_name'], $destination)) {
$this->localPath = $destination;
return true;
}
$this->errors[] = "Failed to move uploaded file.";
return false;
}
public function dump_all()
public function process(): array
{
$array = array(
"file" => $this->file,
"file_name" => $this->file_name,
"file_size" => $this->file_size,
"file_tmp" => $this->file_tmp,
"file_type" => $this->file_type,
"file_ext" => $this->file_ext,
"file_path" => $this->file_path,
"extensions" => $this->extensions,
"upload_dir" => $this->upload_dir,
"errors" => $this->errors,
"ok" => $this->ok,
"uuid" => $this->uuid
);
// Assuming moveFile() has been called
$uploadTask = [
'original_name' => $this->file['name'],
'local_path' => $this->localPath,
'size' => $this->file['size'],
'ext' => $this->fileExt,
];
if ($this->fileExt === "mp3") {
$escapedFile = escapeshellarg($this->localPath);
$artist = trim(shell_exec("eyed3 --query='%a%' $escapedFile"));
$title = trim(shell_exec("eyed3 --query='%t%' $escapedFile"));
$duration = trim(shell_exec("mp3info -p \"%S\" $escapedFile"));
$uploadTask['file_type'] = 'mp3';
$uploadTask['metadata'] = [
'artist' => $artist,
'title' => $title,
'duration' => $duration,
];
$uploadTask['mediaplayer'] = 0;
} elseif ($this->fileExt === "zip") {
$zip = new \ZipArchive;
if ($zip->open($this->localPath) === true) {
$totalDuration = 0;
$tracklist = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$entryName = $zip->getNameIndex($i);
$entryExt = strtolower(pathinfo($entryName, PATHINFO_EXTENSION));
if ($entryExt === "mp3") {
$tempDir = $this->uploadDir . uniqid('zip_');
mkdir($tempDir);
$tempFilePath = $tempDir . '/' . basename($entryName);
$zip->extractTo($tempDir, $entryName);
$escapedFile = escapeshellarg($tempFilePath);
$title = trim(shell_exec("eyed3 --query='%t%' $escapedFile"));
$duration = trim(shell_exec("mp3info -p \"%S\" $escapedFile"));
$tracklist[] = $title ?: basename($entryName);
$totalDuration += (int)$duration;
unlink($tempFilePath);
rmdir($tempDir);
}
}
$zip->close();
$uploadTask['file_type'] = 'zip';
$uploadTask['metadata'] = [
'tracklist' => $tracklist,
'total_duration' => $totalDuration,
];
$uploadTask['mediaplayer'] = 1;
} else {
$this->errors[] = "Failed to open ZIP file.";
}
} else {
$this->errors[] = "Unsupported file type.";
}
return $uploadTask;
}
public function uploadToCDN(CDN $cdn): string
{
// Create a remote path e.g., under a temp folder with a unique name
$remotePath = "temp/mixes/" . uniqid() . "_" . basename($this->localPath);
$mimeType = ($this->fileExt === 'mp3') ? 'audio/mpeg' : 'application/zip';
$cdn->uploadFile($this->localPath, $remotePath, $mimeType);
return $remotePath;
}
}