Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add REST endpint for ffmpeg server callback handler #293

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 79 additions & 42 deletions admin/rt-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RT_Transcoder_Handler {
* @access protected
* @var string $transcoding_api_url The URL of the api.
*/
protected $transcoding_api_url = 'https://api.rtmedia.io/api/v1/';
protected $transcoding_api_url = 'http://frappe-transcoder-api.rt.gw/api/';

/**
* The URL of the EDD store.
Expand All @@ -38,7 +38,7 @@ class RT_Transcoder_Handler {
* @access protected
* @var string $store_url The URL of the transcoder api.
*/
protected $store_url = 'https://rtmedia.io/';
protected $store_url = 'http://frappe-transcoder-api.rt.gw/api/';

/**
* Contain uploaded media information.
Expand Down Expand Up @@ -147,24 +147,24 @@ public function __construct( $no_init = false ) {
$usage_info = get_site_option( 'rt-transcoding-usage' );

if ( isset( $usage_info ) && is_array( $usage_info ) && array_key_exists( $this->api_key, $usage_info ) ) {
if ( isset( $usage_info[ $this->api_key ]->plan->expires )
&& strtotime( $usage_info[ $this->api_key ]->plan->expires ) < time() ) {
$usage_info = $this->update_usage( $this->api_key );
}
// if ( isset( $usage_info[ $this->api_key ]->plan->expires )
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
// && strtotime( $usage_info[ $this->api_key ]->plan->expires ) < time() ) {
// $usage_info = $this->update_usage( $this->api_key );
// }
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
if ( array_key_exists( $this->api_key, $usage_info ) && is_object( $usage_info[ $this->api_key ] ) && isset( $usage_info[ $this->api_key ]->status ) && $usage_info[ $this->api_key ]->status ) {
if ( isset( $usage_info[ $this->api_key ]->remaining ) && $usage_info[ $this->api_key ]->remaining > 0 ) {

// Enable re-transcoding.
include_once RT_TRANSCODER_PATH . 'admin/rt-retranscode-admin.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant

if ( $usage_info[ $this->api_key ]->remaining < 524288000 && ! get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
$this->nearing_usage_limit( $usage_info );
} elseif ( $usage_info[ $this->api_key ]->remaining > 524288000 && get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
update_site_option( 'rt-transcoding-usage-limit-mail', 0 );
}
if ( strtotime( $usage_info[ $this->api_key ]->plan->expires ) > time() ) {
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
}
// if ( $usage_info[ $this->api_key ]->remaining < 524288000 && ! get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
// $this->nearing_usage_limit( $usage_info );
// } elseif ( $usage_info[ $this->api_key ]->remaining > 524288000 && get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
// update_site_option( 'rt-transcoding-usage-limit-mail', 0 );
// }
// if ( strtotime( $usage_info[ $this->api_key ]->plan->expires ) > time() ) {
// add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
// }
pranvinit marked this conversation as resolved.
Show resolved Hide resolved

/* Do not let the user to upload non supported media types on localhost */
$blacklist = rtt_get_blacklist_ip_addresses();
Expand All @@ -183,6 +183,10 @@ public function __construct( $no_init = false ) {
add_action( 'wp_ajax_rt_disable_transcoding', array( $this, 'disable_transcoding' ), 1 );
add_action( 'wp_ajax_rt_enable_transcoding', array( $this, 'enable_transcoding' ), 1 );
add_action( 'add_attachment', array( $this, 'after_upload_pdf' ) );
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
add_filter( 'rtmedia_plupload_files_filter', array( $this, 'allowed_types' ), 10, 1 );
add_filter( 'rtmedia_allowed_types', array( $this, 'allowed_types_admin_settings' ), 10, 1 );
add_filter( 'rtmedia_valid_type_check', array( $this, 'bypass_video_audio' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -256,23 +260,29 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
}
}

$callback_url = RT_TRANSCODER_CALLBACK_URL;

if ( ! defined( 'RT_TRANSCODER_CALLBACK_URL' ) || empty( RT_TRANSCODER_CALLBACK_URL ) ) {
return;
}

$args = array(
'method' => 'POST',
'sslverify' => false,
'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'api_token' => $this->api_key,
'job_type' => $job_type,
'job_for' => $job_for,
'file_url' => rawurlencode( $url ),
'callback_url' => rawurlencode( trailingslashit( home_url() ) . 'index.php' ),
'force' => 0,
'formats' => ( true === $autoformat ) ? ( ( 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
'thumb_count' => $options_video_thumb,
'api_token' => $this->api_key,
'job_type' => $job_type,
'job_for' => $job_for,
'file_origin' => rawurlencode( $url ),
'callback_url' => rawurlencode( $callback_url ),
'force' => 0,
'formats' => ( true === $autoformat ) ? ( ( 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
'thumbnail_count' => $options_video_thumb,
),
);

$transcoding_url = $this->transcoding_api_url . 'job/';
$transcoding_url = $this->transcoding_api_url . 'resource/Transcoder Job';

$upload_page = wp_remote_post( $transcoding_url, $args );

Expand All @@ -283,8 +293,9 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
)
) {
$upload_info = json_decode( $upload_page['body'] );
if ( isset( $upload_info->status ) && $upload_info->status && isset( $upload_info->job_id ) && $upload_info->job_id ) {
$job_id = $upload_info->job_id;
error_log( json_encode( $upload_info ) );
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
if ( isset( $upload_info->data ) && isset( $upload_info->data->name ) ) {
$job_id = $upload_info->data->name;
update_post_meta( $attachment_id, '_rt_transcoding_job_id', $job_id );
}
}
Expand Down Expand Up @@ -350,16 +361,16 @@ public function bypass_video_audio( $flag, $file ) {
* @return boolean $status If true then key is valid else key is not valid.
*/
public function is_valid_key( $key ) {
$validate_url = trailingslashit( $this->store_url ) . 'rt-eddsl-api/?rt-eddsl-license-key=' . $key;
$validate_url = trailingslashit( $this->store_url ) . '/resource/Transcoder License/' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$validation_page = vip_safe_wp_remote_get( $validate_url, '', 3, 3 );
} else {
$validation_page = wp_safe_remote_get( $validate_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $validation_page ) ) {
$validation_info = json_decode( $validation_page['body'] );
if ( isset( $validation_info->status ) ) {
$status = $validation_info->status;
if ( isset( $validation_info->data->status ) && 'Active' === $validation_info->data->status ) {
$status = true;
}
} else {
$status = false;
Expand All @@ -378,14 +389,15 @@ public function is_valid_key( $key ) {
* @return array $usage_info An array containing usage information.
*/
public function update_usage( $key ) {
$usage_url = trailingslashit( $this->transcoding_api_url ) . 'usage/' . $key;
$usage_url = trailingslashit( $this->transcoding_api_url ) . 'resource/Transcoder License/' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$usage_page = vip_safe_wp_remote_get( $usage_url, '', 3, 3 );
} else {
$usage_page = wp_safe_remote_get( $usage_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $usage_page ) ) {
$usage_info = json_decode( $usage_page['body'] );
$usage_info = $usage_info->data;
} else {
$usage_info = null;
}
Expand Down Expand Up @@ -463,7 +475,7 @@ public function usage_quota_over() {
*/
public function save_api_key() {
$is_api_key_updated = transcoder_filter_input( INPUT_GET, 'api-key-updated', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_invalid_license_key = transcoder_filter_input( INPUT_GET, 'invalid-license-key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_invalid_license_key = false;
$is_localhost = transcoder_filter_input( INPUT_GET, 'need-public-host', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

if ( $is_api_key_updated ) {
Expand Down Expand Up @@ -514,7 +526,7 @@ public function save_api_key() {
$return_page = add_query_arg(
array(
'page' => 'rt-transcoder',
'api-key-updated' => $usage_info->plan->name ? ucfirst( strtolower( $usage_info->plan->name ) ) : 'Free',
'api-key-updated' => $usage_info->plan ? ucfirst( strtolower( $usage_info->plan ) ) : 'Free',
),
admin_url( 'admin.php' )
);
Expand Down Expand Up @@ -1111,6 +1123,7 @@ public function get_post_id_by_meta_key_and_value( $key, $value ) {
* @since 1.0.0
*/
public function handle_callback() {
// TODO: Refactor this function into REST API.
require_once ABSPATH . 'wp-admin/includes/image.php';

$job_id = transcoder_filter_input( INPUT_POST, 'job_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
Expand Down Expand Up @@ -1610,20 +1623,20 @@ class_exists( 'ImagickPixel', false ) &&
/**
* Sanitize transcoder post respopnse array.
*/
private function filter_transcoder_response() {
public function filter_transcoder_response() {
$post_var = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing

$filter_post_args = array(
'job_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_for' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'format' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'download_url' => FILTER_SANITIZE_URL,
'file_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'thumb_count' => FILTER_SANITIZE_NUMBER_INT,
'status' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_msg' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_code' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_for' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'format' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'download_url' => FILTER_SANITIZE_URL,
'file_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'thumbnail_count' => FILTER_SANITIZE_NUMBER_INT,
'status' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_msg' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_code' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
);

$post_array = filter_input_array( INPUT_POST, $filter_post_args );
Expand All @@ -1643,4 +1656,28 @@ private function filter_transcoder_response() {

return $post_array;
}

/**
* Sanitize transcoder post response array for JSON.
*
* @param WP_REST_Request $request Request object.
*
* @return array $post_array Sanitized post array.
*/
public function filter_transcoder_response_json( $request ) {
return array(
'job_id' => sanitize_text_field( wp_unslash( $request->get_param( 'job_id' ) ) ),
'job_type' => sanitize_text_field( wp_unslash( $request->get_param( 'job_type' ) ) ),
'job_for' => sanitize_text_field( wp_unslash( $request->get_param( 'job_for' ) ) ),
'format' => sanitize_text_field( wp_unslash( $request->get_param( 'format' ) ) ),
'download_url' => esc_url_raw( $request->get_param( 'download_url' ) ),
'file_name' => sanitize_text_field( wp_unslash( $request->get_param( 'file_name' ) ) ),
'thumb_count' => absint( $request->get_param( 'thumbnail_count' ) ),
'status' => sanitize_text_field( wp_unslash( $request->get_param( 'status' ) ) ),
'files' => array_map( 'esc_url_raw', (array) $request->get_param( 'files' ) ),
'file_status' => sanitize_text_field( wp_unslash( $request->get_param( 'file_status' ) ) ),
'thumbnail' => array_map( 'esc_url_raw', (array) $request->get_param( 'thumbnail' ) ),
'error_msg' => sanitize_text_field( wp_unslash( $request->get_param( 'error_msg' ) ) ),
);
}
}
Loading
Loading