Enhance Gitea support and ensure UTF-8 encoding in API requests
This commit is contained in:
@@ -341,14 +341,15 @@ function oribi_sync_fetch_file( string $api_base, string $branch, string $file_p
|
||||
break;
|
||||
|
||||
case 'gitea':
|
||||
$url = $api_base . '/raw/' . $encoded_path . '?ref=' . rawurlencode( $branch );
|
||||
$accept = 'text/plain';
|
||||
// Use /contents/ endpoint which returns base64-encoded content (more reliable)
|
||||
$url = $api_base . '/contents/' . $encoded_path . '?ref=' . rawurlencode( $branch );
|
||||
$accept = 'application/json';
|
||||
break;
|
||||
|
||||
case 'github':
|
||||
default:
|
||||
$url = $api_base . '/contents/' . $encoded_path . '?ref=' . rawurlencode( $branch );
|
||||
$accept = 'application/vnd.github.raw+json';
|
||||
$accept = 'application/vnd.github.raw';
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -377,7 +378,28 @@ function oribi_sync_fetch_file( string $api_base, string $branch, string $file_p
|
||||
);
|
||||
}
|
||||
|
||||
return wp_remote_retrieve_body( $response );
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
|
||||
// For Gitea, the /contents/ endpoint returns base64-encoded content in JSON
|
||||
if ( $provider === 'gitea' ) {
|
||||
$decoded = json_decode( $body, true );
|
||||
if ( isset( $decoded['content'] ) && is_string( $decoded['content'] ) ) {
|
||||
$body = base64_decode( $decoded['content'], true );
|
||||
if ( $body === false ) {
|
||||
return new WP_Error( 'oribi_sync_decode_error', 'Failed to decode base64 content from Gitea' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate and fix encoding if necessary (handles non-UTF-8 sources)
|
||||
if ( ! empty( $body ) ) {
|
||||
if ( ! mb_check_encoding( $body, 'UTF-8' ) ) {
|
||||
// Try to convert from common encodings to UTF-8
|
||||
$body = mb_convert_encoding( $body, 'UTF-8', 'UTF-8, ISO-8859-1, Windows-1252' );
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
// ─── Tree filtering ───────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user