Skip to content
Merged
7 changes: 6 additions & 1 deletion admin/class-rtgodam-retranscodemedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,15 @@ public function admin_enqueues( $hook_suffix ) {
*/
public function add_media_row_action( $actions, $post ) {

// Documents are included so files already in the library — which predate document
// support and so were never sent to GoDAM Central — can have a preview generated
// without re-uploading them. Tested against the attachment rather than its MIME alone,
// so the action is not offered on the .srt/.asc/.c/.h files that share text/plain and
// have no conversion path.
if ( (
'audio/' !== substr( $post->post_mime_type, 0, 6 ) &&
'video/' !== substr( $post->post_mime_type, 0, 6 ) &&
'application/pdf' !== $post->post_mime_type
! rtgodam_is_supported_document_attachment( $post->ID )
) ||
// Safe fallback via filter; PHPCS can't resolve dynamic capability.
// phpcs:ignore WordPress.WP.Capabilities.Undetermined
Expand Down
86 changes: 82 additions & 4 deletions admin/class-rtgodam-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ class RTGODAM_Transcoder_Handler {
*/
public $other_extensions = ',pdf';

/**
* Document extensions with comma separated.
*
* Office / OpenDocument / plain-text formats, which GoDAM Central converts to a preview
* PDF. They are kept apart from $other_extensions because those map the extension
* straight onto the job type ('pdf' => job_type 'pdf'), whereas every format here shares
* the single job type 'document'.
*
* No leading comma, unlike $audio_extensions and $other_extensions: exploding those yields
* an empty first entry, which then matches a file with no extension at all.
*
* @since n.e.x.t
* @access public
* @var string $document_extensions Document extensions with comma separated.
*/
public $document_extensions = 'docx,doc,xlsx,xls,pptx,ppt,odt,ods,odp,txt,csv';

/**
* Allowed mimetypes.
*
Expand All @@ -101,7 +118,6 @@ class RTGODAM_Transcoder_Handler {
*/
public $allowed_mimetypes = array(
'application/ogg',
'application/pdf',
);

/**
Expand All @@ -125,6 +141,21 @@ public function __construct( $no_init = false ) {
$this->api_key = get_option( 'rtgodam-api-key' );
$this->easydam_settings = get_option( 'rtgodam-settings', array() );

/*
* Document MIME types come from the shared helper rather than being duplicated in
* the property default, so the transcoder and the Document block can never disagree
* about which formats are supported. Anything not listed here is dropped by the
* mime gate in wp_media_transcoding() and never reaches GoDAM Central.
*/
$this->allowed_mimetypes = array_values(
array_unique(
array_merge(
$this->allowed_mimetypes,
array_keys( rtgodam_get_supported_document_types() )
)
)
);

$default_settings = array(
'video' => array(
'adaptive_bitrate' => false,
Expand Down Expand Up @@ -308,11 +339,31 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat

$metadata = $wp_metadata;

$type_arry = explode( '.', $url );
$type = strtolower( $type_arry[ count( $type_arry ) - 1 ] );
$extension = pathinfo( $path, PATHINFO_EXTENSION );
$type_arry = explode( '.', $url );
$type = strtolower( $type_arry[ count( $type_arry ) - 1 ] );
// Lowercased because the extension lists below are all lowercase: an upload named
// REPORT.PDF would otherwise miss its branch and be sent as a video stream job.
$extension = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) );
$not_allowed_type = array();

/*
* A document MIME type is not sufficient on its own, because several of them are
* shared with formats that have no conversion path. WordPress maps .srt, .asc, .c,
* .cc and .h to text/plain exactly as it maps .txt, so without this a subtitle file
* would satisfy the MIME gate below, find no matching extension in any of the job
* type branches, and fall through to the default 'stream' — dispatching every
* caption upload to GoDAM Central as a video transcode that can only fail.
*
* Checked here rather than inside the gate so audio/video and application/ogg keep
* matching on MIME alone, exactly as they did before documents were supported.
*/
if (
array_key_exists( $metadata['mime_type'], rtgodam_get_supported_document_types() )
&& ! in_array( $extension, rtgodam_get_supported_document_extensions(), true )
) {
return $wp_metadata;
}

if ( (
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array ) ||
in_array( $metadata['mime_type'], $this->allowed_mimetypes, true )
Expand All @@ -334,6 +385,20 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
$job_type = $extension;
$autoformat = $extension;
$options_video_thumb = 0;
} elseif ( '' !== $extension && in_array( $extension, explode( ',', $this->document_extensions ), true ) ) {
/*
* Office / OpenDocument / text files all share one job type. GoDAM Central
* converts them to a preview PDF (job_type 'document' routes to its document
* queue), and returns that PDF separately as `preview_pdf_url` — `download_url`
* stays the original file. `formats` names the conversion target rather than
* the source extension, unlike the 'pdf' branch above where the two coincide.
*
* Thumbnails are rasterised from page 0 of the preview by Central itself, so
* no thumbnail count is requested here.
*/
$job_type = 'document';
$autoformat = 'pdf';
$options_video_thumb = 0;
}

/** Figure out who is requesting this job */
Expand Down Expand Up @@ -445,6 +510,19 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
delete_post_meta( $attachment_id, 'rtgodam_transcoding_error_msg' );
delete_post_meta( $attachment_id, 'rtgodam_transcoding_error_code' );

if ( 'document' === $job_type ) {
/*
* Drop any preview from a previous render of this attachment. The job row
* is reused for a retranscode or an in-place replacement, so the stored
* preview belongs to the file that WAS here — leaving it in place would
* keep serving the old document's contents until the callback arrives, and
* forever if the job fails, since the error path never reaches the
* callback's cleanup. A document with no preview shows the download-only
* panel, and 'Queued' above makes the block show progress meanwhile.
*/
delete_post_meta( $attachment_id, 'rtgodam_preview_pdf_url' );
}

if ( $manual_retranscode ) {
$failed_transcoding_attachments = get_option( 'rtgodam-failed-transcoding-attachments', array() );

Expand Down
69 changes: 67 additions & 2 deletions admin/class-rtgodam-transcoder-rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ public function register_routes() {
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),

/*
* Machine-readable failure reason (DOCUMENT_TOO_LARGE,
* DOCUMENT_UNSUPPORTED_TYPE, DOCUMENT_PROCESSING_FAILED, ...). Unlike
* error_msg it is safe to surface, since it carries no file paths or
* internal detail.
*/
'error_code' => array(
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),

/*
* The converted preview PDF for a `document` job. Read from the top-level
* field rather than from files['preview_pdf']: the callback is form-encoded,
* and a nested array does not survive that encoding reliably.
*
* Absent — not empty — for job types that have no conversion step, and
* empty for a password protected document, which Central stores and
* reports as Transcoded but deliberately generates no preview for.
*/
'preview_pdf_url' => array(
'required' => false,
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
),
'job_manager_form' => array(
'required' => false,
'type' => 'string',
Expand Down Expand Up @@ -227,6 +254,7 @@ public function handle_callback( WP_REST_Request $request ) {
$job_id = $request->get_param( 'job_id' );
$file_status = $request->get_param( 'file_status' );
$error_msg = $request->get_param( 'error_msg' );
$error_code = $request->get_param( 'error_code' );
$job_for = $request->get_param( 'job_for' );
$thumbnail = $request->get_param( 'thumbnail' );
$format = $request->get_param( 'format' );
Expand All @@ -244,6 +272,16 @@ public function handle_callback( WP_REST_Request $request ) {
if ( ! empty( $error_msg ) ) {
update_post_meta( $failed_id, 'rtgodam_transcoding_error_msg', sanitize_textarea_field( $error_msg ) );
}

/*
* The status callback stores this too, but a job that fails outright may
* never send one, and the code is what distinguishes a retryable failure
* (DOCUMENT_PROCESSING_FAILED) from a permanent one (DOCUMENT_TOO_LARGE,
* DOCUMENT_UNSUPPORTED_TYPE) that should not offer a retry.
*/
if ( ! empty( $error_code ) ) {
update_post_meta( $failed_id, 'rtgodam_transcoding_error_code', sanitize_text_field( $error_code ) );
}
}
}
return new WP_REST_Response(
Expand Down Expand Up @@ -306,11 +344,38 @@ public function handle_callback( WP_REST_Request $request ) {
}
}

if ( 'pdf' === $job_type && isset( $post_array['download_url'] ) && ! empty( $post_array['download_url'] ) ) {
// Setting the transcoded PDF URL.
/*
* Documents. `pdf` is a file that needed no conversion; `document` is an
* Office / OpenDocument / text file that GoDAM Central rendered to a preview
* PDF. For both, `download_url` is the file the author uploaded, on the CDN.
*
* The two URLs are NOT interchangeable for a `document`: only the preview may
* be rendered, and only the original may be offered as a download. Somebody
* who uploads report.xlsx and downloads preview.pdf will think something broke.
*/
if ( in_array( $job_type, array( 'pdf', 'document' ), true ) && ! empty( $post_array['download_url'] ) ) {
update_post_meta( $attachment_id, 'rtgodam_transcoded_url', esc_url_raw( $post_array['download_url'] ) );
}

if ( 'document' === $job_type ) {
if ( ! empty( $post_array['preview_pdf_url'] ) ) {
update_post_meta( $attachment_id, 'rtgodam_preview_pdf_url', esc_url_raw( $post_array['preview_pdf_url'] ) );
} else {
Comment on lines +360 to +363
/*
* No preview: the document is password protected, so Central stored it
* as-is and generated nothing to render. Deleting rather than skipping
* matters — a retranscode or in-place replace reuses the same job row,
* and leaving the key alone would keep serving the *previous* render's
* preview for a file that no longer has one.
*/
delete_post_meta( $attachment_id, 'rtgodam_preview_pdf_url' );
}
} elseif ( 'pdf' === $job_type && ! empty( $post_array['download_url'] ) ) {
// A PDF is its own preview, so both keys point at the same file. Storing it
// here as well means every reader has a single key to consult.
update_post_meta( $attachment_id, 'rtgodam_preview_pdf_url', esc_url_raw( $post_array['download_url'] ) );
}

if ( 'image' === $job_type && isset( $post_array['download_url'] ) && ! empty( $post_array['download_url'] ) ) {
// Setting the transcoded Image URL.
update_post_meta( $attachment_id, 'rtgodam_transcoded_url', esc_url_raw( $post_array['download_url'] ) );
Expand Down
4 changes: 3 additions & 1 deletion admin/godam-transcoder-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ function rtgodam_add_transcoded_url_field( $form_fields, $post ) {
// Check if attachment is of type video.
$mime_type = get_post_mime_type( $post->ID );

// The document test goes through the attachment, not the MIME alone: text/plain also
// covers .srt/.asc/.c/.h, which are never transcoded and so have no CDN URL to show.
$is_allowed = (
str_starts_with( $mime_type, 'video/' ) ||
str_starts_with( $mime_type, 'audio/' ) ||
str_starts_with( $mime_type, 'image/' ) ||
'application/pdf' === $mime_type
rtgodam_is_supported_document_attachment( $post->ID )
);

if ( ! $is_allowed ) {
Expand Down
4 changes: 2 additions & 2 deletions assets/src/blocks/godam-pdf/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "godam/pdf",
"title": "Document",
"category": "media",
"description": "Embed a PDF file with preview.",
"keywords": [ "document", "pdf", "file", "preview", "godam" ],
"description": "Embed a document with a preview. Supports PDF, Word, Excel, PowerPoint, OpenDocument, TXT and CSV.",
"keywords": [ "document", "pdf", "doc", "docx", "word", "excel", "xlsx", "powerpoint", "pptx", "spreadsheet", "csv", "file", "preview", "godam" ],
"textdomain": "godam",
"attributes": {
"blob": {
Expand Down
Loading
Loading