Enhance Gitea support by improving base64 decoding and UTF-8 encoding handling

This commit is contained in:
Matt Batchelder
2026-02-21 20:08:18 -05:00
parent 634e93236f
commit 3b51382797
4 changed files with 27 additions and 13 deletions

View File

@@ -104,7 +104,7 @@ function oribi_sync_api_request( string $method, string $url, array $body, strin
'method' => $method,
'timeout' => 30,
'headers' => $headers,
'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE ),
'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ),
];
$response = wp_remote_request( $url, $args );
@@ -152,9 +152,15 @@ function oribi_sync_gitea_get_file_meta( string $api_base, string $branch, strin
return $result;
}
// Gitea inserts \n every 60 chars in base64 — strip before decoding.
$raw_b64 = $result['content'] ?? '';
$content = ! empty( $raw_b64 )
? base64_decode( str_replace( [ "\r", "\n", " " ], '', $raw_b64 ), true )
: '';
return [
'sha' => $result['sha'] ?? '',
'content' => isset( $result['content'] ) ? base64_decode( $result['content'] ) : '',
'content' => ( $content !== false ) ? $content : '',
];
}
@@ -182,6 +188,11 @@ function oribi_sync_gitea_put_file(
?string $sha = null,
string $message = ''
) {
// Validate and fix UTF-8 encoding before base64-encoding
if ( ! mb_check_encoding( $content, 'UTF-8' ) ) {
$content = mb_convert_encoding( $content, 'UTF-8', 'UTF-8, ISO-8859-1, Windows-1252' );
}
$encoded_path = implode( '/', array_map( 'rawurlencode', explode( '/', $filepath ) ) );
$url = $api_base . '/contents/' . $encoded_path;