public function PreProcess() { $v = Validator::Create(); $v->Register($this->source[Video_Source::FIELD_EMBED], Validator_Type::NOT_EMPTY, 'The Embed Code field is required'); $v->Register($this->source[Video_Source::FIELD_DURATION], Validator_Type::VALID_TIME, 'The Video Duration field must be in HH:MM:SS format'); $this->duration = Format::DurationToSeconds($this->source[Video_Source::FIELD_DURATION]); $this->video_dir = new Video_Dir(null, 0700); Request::FixFiles(); // No thumbnails uploaded if (!isset($_FILES[Video_Source::FIELD_THUMBNAILS])) { return; } // Process each uploaded file foreach ($_FILES[Video_Source::FIELD_THUMBNAILS] as $upload) { // No file uploaded in this field if ($upload['error'] == UPLOAD_ERR_NO_FILE) { continue; } // Check for other errors if ($upload['error'] != UPLOAD_ERR_OK) { throw new BaseException(Uploads::CodeToMessage($upload['error'])); } switch (File::Type($upload['name'])) { case File::TYPE_ZIP: foreach (Zip::ExtractEntries($upload['tmp_name'], File::TYPE_JPEG) as $name => $data) { $thumbs[] = $this->video_dir->AddTempFromVar($data, JPG_EXTENSION); } break; case File::TYPE_JPEG: $thumbs[] = $this->video_dir->AddTempFromFile($upload['tmp_name'], JPG_EXTENSION); break; } } // Resize (if possible) and move images to the correct directory if (Video_Thumbnail::CanResize()) { $this->thumbs = Video_Thumbnail::ResizeDirectory($this->video_dir->GetTempDir(), $this->video_dir->GetThumbsDir(), Config::Get('thumb_size'), Config::Get('thumb_quality')); } else { $this->thumbs = $this->video_dir->MoveFiles(Video_Dir::TEMP, Video_Dir::THUMBS, JPG_EXTENSION); } // Cleanup temp and processing dirs $this->video_dir->ClearTemp(); $this->video_dir->ClearProcessing(); }
public function PreProcess() { $this->video_dir = new Video_Dir(null, 0700); Request::FixFiles(); if (!isset($_FILES[Video_Source::FIELD_UPLOADS])) { throw new BaseException('No files were uploaded'); } foreach ($_FILES[Video_Source::FIELD_UPLOADS] as $upload) { // No file uploaded in this field if ($upload['error'] == UPLOAD_ERR_NO_FILE) { continue; } // Check for other errors if ($upload['error'] != UPLOAD_ERR_OK) { throw new BaseException(Uploads::CodeToMessage($upload['error'])); } $thumbs = array(); $will_grab = Video_Info::CanExtract() && Video_FrameGrabber::CanGrab(); switch (File::Type($upload['name'])) { case File::TYPE_ZIP: foreach (Zip::ExtractEntries($upload['tmp_name'], File::TYPE_JPEG) as $name => $data) { $thumbs[] = $this->video_dir->AddTempFromVar($data, JPG_EXTENSION); } foreach (Zip::ExtractEntries($upload['tmp_name'], File::TYPE_VIDEO) as $name => $data) { $this->clips[] = $this->video_dir->AddClipFromVar($data, File::Extension($name)); } break; case File::TYPE_JPEG: $thumbs[] = $this->video_dir->AddTempFromFile($upload['tmp_name'], JPG_EXTENSION); break; case File::TYPE_VIDEO: $this->clips[] = $this->video_dir->AddClipFromFile($upload['tmp_name'], File::Extension($upload['name'])); break; } } // Make sure at least one video clip was uploaded if (empty($this->clips)) { throw new BaseException('No video files were uploaded'); } // Try to grab frames from video files if ($will_grab) { $amount = round(Config::Get('thumb_amount') / count($this->clips)); foreach ($this->clips as $clip) { $vi = new Video_Info($clip); $vi->Extract(); $this->duration += $vi->length; $temp_thumbs = Video_FrameGrabber::Grab($clip, $this->video_dir->GetProcessingDir(), $amount, Config::Get('thumb_quality'), Config::Get('thumb_size')); // Move generated thumbs from the processing directory foreach ($temp_thumbs as $temp_thumb) { $this->thumbs[] = $this->video_dir->AddThumbFromFile($temp_thumb); } $this->video_dir->ClearProcessing(); } } else { $this->duration = $this->source[Video_Source::FIELD_DURATION]; } // Use uploaded images if none could be generated if (empty($this->thumbs) && !empty($thumbs)) { if (Video_Thumbnail::CanResize()) { $this->thumbs = Video_Thumbnail::ResizeDirectory($this->video_dir->GetTempDir(), $this->video_dir->GetThumbsDir(), Config::Get('thumb_size'), Config::Get('thumb_quality')); } else { $this->thumbs = $this->video_dir->MoveFiles(Video_Dir::TEMP, Video_Dir::THUMBS, JPG_EXTENSION); } } // Cleanup temp and processing dirs $this->video_dir->ClearTemp(); $this->video_dir->ClearProcessing(); }
function tbxVideoEdit() { Privileges::Check(Privileges::VIDEOS); $DB = GetDB(); $schema = GetDBSchema(); $v = Validator::Create(); $v->RegisterFromXml($schema->el('//table[name="tbx_video"]')); $v->RegisterFromXml($schema->el('//table[name="tbx_video_custom"]')); if (!String::IsEmpty($_REQUEST['username'])) { $v->Register($DB->QueryCount('SELECT COUNT(*) FROM `tbx_user` WHERE `username`=?', array($_REQUEST['username'])) > 0, Validator_Type::IS_TRUE, 'The Username entered does not exist'); } if (!$v->Validate()) { return JSON::Failure(array('message' => 'Video could not be updated; please fix the following items', 'errors' => $v->GetErrors())); } $_REQUEST['display_thumbnail'] = empty($_REQUEST['display_thumbnail']) ? null : $_REQUEST['display_thumbnail']; $_REQUEST['date_recorded'] = String::Nullify($_REQUEST['date_recorded']); $_REQUEST['tags'] = Tags::Format($_REQUEST['tags']); $_REQUEST['username'] = String::Nullify(Request::Get('username')); $_REQUEST['duration'] = Format::DurationToSeconds($_REQUEST['duration']); $original = $DB->Row('SELECT * FROM `tbx_video` WHERE `video_id`=?', array($_REQUEST['video_id'])); // Handle uploaded thumbs, if any $dir = new Video_Dir(Video_Dir::DirNameFromId($original['video_id'])); $thumbs_added = 0; $thumb_ids = array(); Request::FixFiles(); if (isset($_FILES['thumb_uploads'])) { foreach ($_FILES['thumb_uploads'] as $upload) { if (File::Extension($upload['name']) == JPG_EXTENSION && ($imgsize = getimagesize($upload['tmp_name'])) !== false) { $temp_file = $dir->AddTempFromFile($upload['tmp_name'], JPG_EXTENSION); if (Video_Thumbnail::CanResize()) { $temp_file = Video_Thumbnail::Resize($temp_file, Config::Get('thumb_size'), Config::Get('thumb_quality'), $dir->GetTempDir()); } $thumb = $dir->AddThumbFromFile($temp_file); $thumbs_added++; $thumb = str_replace(Config::Get('document_root'), '', $thumb); $thumb_ids[] = array('uri' => $thumb, 'id' => DatabaseAdd('tbx_video_thumbnail', array('video_id' => $original['video_id'], 'thumbnail' => $thumb))); } } if ($thumbs_added > 0) { $dir->ClearTemp(); $_REQUEST['num_thumbnails'] = $original['num_thumbnails'] + $thumbs_added; } } // Update base database tables $video = DatabaseUpdate('tbx_video', $_REQUEST); DatabaseUpdate('tbx_video_custom', $_REQUEST); // Handle changes to video clips foreach ($_REQUEST['clips'] as $clip_id => $clip) { DatabaseUpdate('tbx_video_clip', array('video_id' => $video['video_id'], 'clip_id' => $clip_id, 'clip' => $clip['clip'])); } if ($_REQUEST['is_private'] && !$original['is_private']) { $_REQUEST['private_id'] = sha1(uniqid(mt_rand(), true)); DatabaseAdd('tbx_video_private', $_REQUEST); if ($original['status'] == STATUS_ACTIVE) { Tags::RemoveFromFrequency($original['tags']); } } else { if (!$_REQUEST['is_private']) { if ($original['status'] == STATUS_ACTIVE) { if ($original['is_private']) { Tags::AddToFrequency($_REQUEST['tags']); } else { Tags::UpdateFrequency($original['tags'], $_REQUEST['tags']); } } $DB->Update('DELETE FROM `tbx_video_private` WHERE `video_id`=?', array($_REQUEST['video_id'])); } } if ($original['status'] == STATUS_ACTIVE) { $t = new Template(); $t->ClearCache('categories.tpl'); } UpdateCategoryStats($original['category_id'], $video['category_id']); UpdateSponsorStats($original['sponsor_id'], $_REQUEST['sponsor_id']); $output = array('id' => $video['video_id'], 'message' => 'Video has been successfully updated', 'html' => SearchItemHtml('video', $video), 'thumbs' => $thumb_ids); JSON::Success($output); }