Enhance Gitea support and ensure UTF-8 encoding in API requests

This commit is contained in:
Matt Batchelder
2026-02-21 19:02:26 -05:00
parent 6c5e503eb2
commit 634e93236f
3 changed files with 35 additions and 6 deletions

Binary file not shown.

View File

@@ -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 ───────────────────────────────────────────────────────────

View File

@@ -87,17 +87,24 @@ function oribi_sync_api_request( string $method, string $url, array $body, strin
$headers = array_merge(
oribi_sync_auth_headers( $provider, $pat ),
[
'Content-Type' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'Accept' => 'application/json',
'User-Agent' => 'Oribi-Sync-WP/' . ORIBI_SYNC_VERSION,
]
);
// Ensure UTF-8 encoding of all body content
array_walk_recursive( $body, function ( &$item ) {
if ( is_string( $item ) && ! mb_check_encoding( $item, 'UTF-8' ) ) {
$item = mb_convert_encoding( $item, 'UTF-8' );
}
});
$args = [
'method' => $method,
'timeout' => 30,
'headers' => $headers,
'body' => wp_json_encode( $body ),
'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE ),
];
$response = wp_remote_request( $url, $args );