%PDF- %PDF-
| Direktori : /home2/rezakur1/yt.rezakurnia.com/api/ |
| Current File : /home2/rezakur1/yt.rezakurnia.com/api/upload.php |
<?php
/**
* POST /api/upload.php
* Form-data:
* - video : file video (wajib)
* - title : judul video (wajib)
* - video_id : (opsional) custom ID
*/
require_once __DIR__ . '/../config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
jsonError('Method not allowed', 405);
}
// Validasi file
if (!isset($_FILES['video']) || $_FILES['video']['error'] !== UPLOAD_ERR_OK) {
$errCode = $_FILES['video']['error'] ?? UPLOAD_ERR_NO_FILE;
jsonError('File video wajib di-upload. Error code: ' . $errCode);
}
$file = $_FILES['video'];
$title = trim($_POST['title'] ?? '');
$videoId = trim($_POST['video_id'] ?? '');
if ($title === '') {
jsonError('Field "title" wajib diisi');
}
// Cek ukuran
if ($file['size'] > MAX_FILE_SIZE) {
jsonError('File terlalu besar (maks ' . (MAX_FILE_SIZE / 1024 / 1024) . 'MB)', 413);
}
// Cek ekstensi
$allowedExt = ['mp4', 'mov', 'avi', 'mkv', 'webm', 'm4v'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $allowedExt)) {
jsonError('Hanya file video yang diperbolehkan (mp4, mov, avi, mkv, webm, m4v)');
}
// Generate nama file unik
ensureUploadDir();
$timestamp = time();
$safeName = preg_replace('/[^a-zA-Z0-9._-]/', '_', pathinfo($file['name'], PATHINFO_FILENAME));
$newName = "{$timestamp}_{$safeName}.{$ext}";
$targetPath = UPLOAD_DIR . '/' . $newName;
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
jsonError('Gagal menyimpan file ke folder uploads', 500);
}
// Generate URL akses
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$fileUrl = "{$scheme}://{$host}" . UPLOAD_URL_PREFIX . "/{$newName}";
// Generate video_id kalau tidak dikasih
if ($videoId === '') {
$videoId = 'local_' . $timestamp;
}
// Simpan metadata ke Supabase
$result = supabaseRequest('videos', 'POST', [
'video_id' => $videoId,
'title' => $title,
'link_shorts' => $fileUrl,
'is_copied' => false,
]);
if (!$result['success']) {
// Rollback: hapus file
@unlink($targetPath);
jsonError('Gagal simpan ke database: ' . $result['error'], $result['code']);
}
$inserted = $result['data'][0] ?? [];
jsonResponse([
'success' => true,
'message' => 'Video berhasil di-upload',
'data' => array_merge($inserted, [
'file_path' => $targetPath,
'file_url' => UPLOAD_URL_PREFIX . "/{$newName}",
'file_size' => $file['size'],
'file_size_mb' => round($file['size'] / 1024 / 1024, 2) . ' MB',
]),
], 201);