| Server IP : 158.247.231.215 / Your IP : 216.73.216.204 Web Server : Apache/2.4.41 (Ubuntu) System : Linux CTMS 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.30 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /mnt/blockstorage/ctms/api/custom-api/ |
Upload File : |
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Load WordPress and auth helper
require_once('/mnt/blockstorage/ctms/wp-load.php');
require_once('auth-helper.php');
include('../database.php');
// Verify JWT token
$user_id = verify_jwt_token();
if (!$user_id) {
send_error_response('User not authenticated', 401);
}
/**
* Upload APK file
*/
function upload_apk($file, $content_id, $version_number, $device_type) {
if (!isset($file) || $file['error'] !== UPLOAD_ERR_OK) {
if ($file['error'] === UPLOAD_ERR_NO_FILE) {
return array('success' => true, 'path' => null, 'size' => 0, 'url' => null); // Optional file
}
return array('success' => false, 'message' => 'File upload error: ' . $file['error']);
}
// Validate file type (APK)
$allowed_extensions = array('apk');
$file_extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($file_extension, $allowed_extensions)) {
return array('success' => false, 'message' => 'Only APK files are allowed');
}
// Create directory for version
$version_dir = '/mnt/blockstorage/ctms/vr-content-uploads/contents/' . $content_id . '/versions/' . $version_number;
if (!file_exists($version_dir)) {
mkdir($version_dir, 0755, true);
chown($version_dir, 'www-data');
chgrp($version_dir, 'www-data');
}
// Generate filename
$filename = $device_type . '.apk';
$file_path = $version_dir . '/' . $filename;
// Move uploaded file
if (!move_uploaded_file($file['tmp_name'], $file_path)) {
return array('success' => false, 'message' => 'Failed to move uploaded file');
}
// Set permissions
chmod($file_path, 0644);
chown($file_path, 'www-data');
chgrp($file_path, 'www-data');
// Get file size
$file_size = filesize($file_path);
// Return relative path
$relative_path = 'contents/' . $content_id . '/versions/' . $version_number . '/' . $filename;
$url = 'https://training.contentsda.kr/vr-content-uploads/' . $relative_path;
return array('success' => true, 'path' => $relative_path, 'size' => $file_size, 'url' => $url);
}
// Main processing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
// Get POST data
$content_id = isset($_POST['content_id']) ? intval($_POST['content_id']) : 0;
$version_number = isset($_POST['version_number']) ? $conn->real_escape_string(trim($_POST['version_number'])) : '';
$release_notes = isset($_POST['release_notes']) ? $conn->real_escape_string(trim($_POST['release_notes'])) : '';
// Validate required fields
if ($content_id <= 0) {
throw new Exception('Valid content_id is required');
}
if (empty($version_number)) {
throw new Exception('version_number is required');
}
// Validate version number format (e.g., 1.0.0)
if (!preg_match('/^\d+\.\d+\.\d+$/', $version_number)) {
throw new Exception('Invalid version number format. Use format like 1.0.0');
}
// Check if content exists
$check_content_sql = "SELECT id FROM vr_contents WHERE id = $content_id";
$check_content_result = $conn->query($check_content_sql);
if (!$check_content_result || $check_content_result->num_rows === 0) {
throw new Exception('Content not found');
}
// Check if version already exists
$check_version_sql = "SELECT id FROM vr_content_versions WHERE content_id = $content_id AND version_number = '$version_number'";
$check_version_result = $conn->query($check_version_sql);
if ($check_version_result && $check_version_result->num_rows > 0) {
throw new Exception('Version ' . $version_number . ' already exists for this content');
}
// Upload Pico APK
$pico_result = array('success' => true, 'path' => null, 'size' => 0, 'url' => null);
if (isset($_FILES['pico_apk'])) {
$pico_result = upload_apk($_FILES['pico_apk'], $content_id, $version_number, 'pico');
if (!$pico_result['success']) {
throw new Exception('Pico APK upload failed: ' . $pico_result['message']);
}
}
// Upload Quest APK
$quest_result = array('success' => true, 'path' => null, 'size' => 0, 'url' => null);
if (isset($_FILES['quest_apk'])) {
$quest_result = upload_apk($_FILES['quest_apk'], $content_id, $version_number, 'quest');
if (!$quest_result['success']) {
throw new Exception('Quest APK upload failed: ' . $quest_result['message']);
}
}
// At least one APK must be provided
if (!$pico_result['path'] && !$quest_result['path']) {
throw new Exception('At least one APK file (Pico or Quest) must be provided');
}
// Insert version record
$pico_path = $pico_result['path'] ? "'" . $conn->real_escape_string($pico_result['path']) . "'" : 'NULL';
$pico_size = $pico_result['size'] ? intval($pico_result['size']) : 'NULL';
$quest_path = $quest_result['path'] ? "'" . $conn->real_escape_string($quest_result['path']) . "'" : 'NULL';
$quest_size = $quest_result['size'] ? intval($quest_result['size']) : 'NULL';
$sql = "INSERT INTO vr_content_versions
(content_id, version_number, pico_apk_path, pico_apk_size, quest_apk_path, quest_apk_size, release_notes, status)
VALUES ($content_id, '$version_number', $pico_path, $pico_size, $quest_path, $quest_size, '$release_notes', 'active')";
if ($conn->query($sql) !== TRUE) {
throw new Exception('Failed to create version: ' . $conn->error);
}
$version_id = $conn->insert_id;
// Success response
$response = array(
'status' => 'success',
'message' => 'Version created successfully',
'data' => array(
'version_id' => $version_id,
'content_id' => $content_id,
'version_number' => $version_number,
'pico_apk_url' => $pico_result['url'],
'pico_apk_size' => $pico_result['size'],
'quest_apk_url' => $quest_result['url'],
'quest_apk_size' => $quest_result['size'],
'release_notes' => $release_notes
)
);
header('Content-Type: application/json');
echo json_encode($response);
} catch (Exception $e) {
// Rollback: delete uploaded files
if (isset($content_id) && isset($version_number)) {
$version_dir = '/mnt/blockstorage/ctms/vr-content-uploads/contents/' . $content_id . '/versions/' . $version_number;
if (file_exists($version_dir)) {
array_map('unlink', glob("$version_dir/*.*"));
rmdir($version_dir);
}
}
$response = array(
'status' => 'error',
'message' => $e->getMessage()
);
header('Content-Type: application/json');
http_response_code(400);
echo json_encode($response);
}
$conn->close();
} else {
$response = array(
'status' => 'error',
'message' => 'Only POST requests are allowed'
);
header('Content-Type: application/json');
http_response_code(405);
echo json_encode($response);
}
?>