Skip to content
Merged
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
23 changes: 20 additions & 3 deletions src/services/github-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global Buffer */
import { requestUrl, RequestUrlResponse, RequestUrlParam } from 'obsidian';
import { GitServiceInterface } from './git-service-interface';

Expand Down Expand Up @@ -88,7 +87,7 @@
}

const data = (response.json as unknown) as GitHubFileResponse;
const decodedContent = Buffer.from(data.content, 'base64').toString('utf8');
const decodedContent = this.fromBase64(data.content);

return {
content: decodedContent,
Expand All @@ -103,7 +102,7 @@

const body: Record<string, unknown> = {
message: commitMessage,
content: Buffer.from(content, 'utf8').toString('base64'),
content: this.toBase64(content),
branch
};

Expand Down Expand Up @@ -266,4 +265,22 @@
.filter(item => item.type === 'blob' && item.path.endsWith('.gitignore'))
.map(item => item.path);
}

private toBase64(str: string): string {
const bytes = new TextEncoder().encode(str);
let binary = '';
bytes.forEach((byte) => {
binary += String.fromCharCode(byte);

Check warning on line 273 in src/services/github-service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String.fromCodePoint()` over `String.fromCharCode()`.

See more on https://sonarcloud.io/project/issues?id=firstsun-dev_git-files-sync&issues=AZ3GUmkpNy07TWuQMiz-&open=AZ3GUmkpNy07TWuQMiz-&pullRequest=14
});
return btoa(binary);
}

private fromBase64(base64: string): string {
const binary = atob(base64.replace(/\s/g, ''));
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);

Check warning on line 282 in src/services/github-service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String#codePointAt()` over `String#charCodeAt()`.

See more on https://sonarcloud.io/project/issues?id=firstsun-dev_git-files-sync&issues=AZ3GUmkpNy07TWuQMiz_&open=AZ3GUmkpNy07TWuQMiz_&pullRequest=14
}
return new TextDecoder().decode(bytes);
}
}
26 changes: 22 additions & 4 deletions src/services/gitlab-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Buffer */

import { requestUrl, RequestUrlResponse, RequestUrlParam } from 'obsidian';
import { GitServiceInterface } from './git-service-interface';

Expand Down Expand Up @@ -58,7 +58,7 @@
const status = error.status || 0;
const responseData = error.response;
const text = responseData?.text || (responseData?.json ? JSON.stringify(responseData.json) : '');

// Re-throw as a standardized response-like object if it looks like one
if (status) {
return {
Expand Down Expand Up @@ -94,7 +94,7 @@
}

const data = (response.json as unknown) as GitLabFileResponse;
const decodedContent = Buffer.from(data.content, 'base64').toString('utf8');
const decodedContent = this.fromBase64(data.content);

return {
content: decodedContent,
Expand All @@ -118,7 +118,7 @@
body: JSON.stringify({
branch,
commit_message: commitMessage,
content: Buffer.from(content, 'utf8').toString('base64'),
content: this.toBase64(content),
encoding: 'base64'
})
});
Expand Down Expand Up @@ -239,4 +239,22 @@
.filter(item => item.type === 'blob' && item.path.endsWith('.gitignore'))
.map(item => item.path);
}

private toBase64(str: string): string {
const bytes = new TextEncoder().encode(str);
let binary = '';
bytes.forEach((byte) => {
binary += String.fromCharCode(byte);

Check warning on line 247 in src/services/gitlab-service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String.fromCodePoint()` over `String.fromCharCode()`.

See more on https://sonarcloud.io/project/issues?id=firstsun-dev_git-files-sync&issues=AZ3GUmmkNy07TWuQMi0A&open=AZ3GUmmkNy07TWuQMi0A&pullRequest=14
});
return btoa(binary);
}

private fromBase64(base64: string): string {
const binary = atob(base64.replace(/\s/g, ''));
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);

Check warning on line 256 in src/services/gitlab-service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `String#codePointAt()` over `String#charCodeAt()`.

See more on https://sonarcloud.io/project/issues?id=firstsun-dev_git-files-sync&issues=AZ3GUmmkNy07TWuQMi0B&open=AZ3GUmmkNy07TWuQMi0B&pullRequest=14
}
return new TextDecoder().decode(bytes);
}
}
Loading