Remove debug logging and add file size limit
This commit is contained in:
parent
92765f7a8a
commit
5582691cf7
5 changed files with 62 additions and 47 deletions
|
|
@ -9,6 +9,7 @@ export interface SyncSettings {
|
||||||
isSyncEnabled: boolean;
|
isSyncEnabled: boolean;
|
||||||
displayNoopSyncEvents: boolean;
|
displayNoopSyncEvents: boolean;
|
||||||
minimumLogLevel: LogLevel;
|
minimumLogLevel: LogLevel;
|
||||||
|
maxFileSizeMB: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: SyncSettings = {
|
export const DEFAULT_SETTINGS: SyncSettings = {
|
||||||
|
|
@ -19,5 +20,6 @@ export const DEFAULT_SETTINGS: SyncSettings = {
|
||||||
syncConcurrency: 1,
|
syncConcurrency: 1,
|
||||||
isSyncEnabled: false,
|
isSyncEnabled: false,
|
||||||
displayNoopSyncEvents: false,
|
displayNoopSyncEvents: false,
|
||||||
minimumLogLevel: LogLevel.INFO
|
minimumLogLevel: LogLevel.INFO,
|
||||||
|
maxFileSizeMB: 10
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ export interface FileOperations {
|
||||||
|
|
||||||
read: (path: RelativePath) => Promise<Uint8Array>;
|
read: (path: RelativePath) => Promise<Uint8Array>;
|
||||||
|
|
||||||
|
getFileSize(path: RelativePath): Promise<number>;
|
||||||
|
|
||||||
getModificationTime: (path: RelativePath) => Promise<Date>;
|
getModificationTime: (path: RelativePath) => Promise<Date>;
|
||||||
|
|
||||||
// Create and write the file if it doesn't exist. Otherwise, it has the same behavior as write.
|
// Create and write the file if it doesn't exist. Otherwise, it has the same behavior as write.
|
||||||
|
|
|
||||||
|
|
@ -8,41 +8,32 @@ export class ObsidianFileOperations implements FileOperations {
|
||||||
public constructor(private readonly vault: Vault) {}
|
public constructor(private readonly vault: Vault) {}
|
||||||
|
|
||||||
public async listAllFiles(): Promise<RelativePath[]> {
|
public async listAllFiles(): Promise<RelativePath[]> {
|
||||||
console.log(this.vault);
|
|
||||||
console.log("before getFiles");
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
const files = this.vault.getFiles();
|
const files = this.vault.getFiles();
|
||||||
console.log("after getFiles");
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
console.log(files);
|
|
||||||
return files.map((file) => file.path);
|
return files.map((file) => file.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read(path: RelativePath): Promise<Uint8Array> {
|
public async read(path: RelativePath): Promise<Uint8Array> {
|
||||||
console.log("before readBinary");
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
const result = new Uint8Array(
|
const result = new Uint8Array(
|
||||||
await this.vault.readBinary(this.getAbstractFile(path))
|
await this.vault.readBinary(this.getAbstractFile(path))
|
||||||
);
|
);
|
||||||
console.log("after readBinary");
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getModificationTime(path: RelativePath): Promise<Date> {
|
public async getFileSize(path: RelativePath): Promise<number> {
|
||||||
console.log("before stat");
|
const file = await this.vault.adapter.stat(normalizePath(path));
|
||||||
await sleep(1000);
|
if (!file) {
|
||||||
|
throw new Error(`File not found: ${path}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getModificationTime(path: RelativePath): Promise<Date> {
|
||||||
const file = await this.vault.adapter.stat(normalizePath(path));
|
const file = await this.vault.adapter.stat(normalizePath(path));
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw new Error(`File not found: ${path}`);
|
throw new Error(`File not found: ${path}`);
|
||||||
}
|
}
|
||||||
console.log("after stat");
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
return new Date(file.mtime);
|
return new Date(file.mtime);
|
||||||
}
|
}
|
||||||
|
|
@ -51,19 +42,13 @@ export class ObsidianFileOperations implements FileOperations {
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
newContent: Uint8Array
|
newContent: Uint8Array
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log("before create");
|
|
||||||
await sleep(1000);
|
|
||||||
if (await this.vault.adapter.exists(normalizePath(path))) {
|
if (await this.vault.adapter.exists(normalizePath(path))) {
|
||||||
await this.write(path, new Uint8Array(0), newContent);
|
await this.write(path, new Uint8Array(0), newContent);
|
||||||
console.log("after create");
|
|
||||||
await sleep(1000);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.createParentDirectories(normalizePath(path));
|
await this.createParentDirectories(normalizePath(path));
|
||||||
await this.vault.createBinary(normalizePath(path), newContent);
|
await this.vault.createBinary(normalizePath(path), newContent);
|
||||||
console.log("after create2");
|
|
||||||
await sleep(1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async write(
|
public async write(
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,21 @@ export class Syncer {
|
||||||
SyncType.CREATE,
|
SyncType.CREATE,
|
||||||
SyncSource.PUSH,
|
SyncSource.PUSH,
|
||||||
async () => {
|
async () => {
|
||||||
|
if (
|
||||||
|
(await this.operations.getFileSize(relativePath)) /
|
||||||
|
1024 /
|
||||||
|
1024 >
|
||||||
|
this.database.getSettings().maxFileSizeMB
|
||||||
|
) {
|
||||||
|
this.history.addHistoryEntry({
|
||||||
|
status: SyncStatus.ERROR,
|
||||||
|
relativePath,
|
||||||
|
message: `File size exceeds the maximum file size limit of ${this.database.getSettings().maxFileSizeMB}MB`,
|
||||||
|
type: SyncType.CREATE
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const contentBytes =
|
const contentBytes =
|
||||||
optimisations?.contentBytes ??
|
optimisations?.contentBytes ??
|
||||||
(await this.operations.read(relativePath));
|
(await this.operations.read(relativePath));
|
||||||
|
|
@ -301,10 +316,25 @@ export class Syncer {
|
||||||
SyncType.UPDATE,
|
SyncType.UPDATE,
|
||||||
SyncSource.PUSH,
|
SyncSource.PUSH,
|
||||||
async () => {
|
async () => {
|
||||||
|
if (
|
||||||
|
(await this.operations.getFileSize(relativePath)) /
|
||||||
|
1024 /
|
||||||
|
1024 >
|
||||||
|
this.database.getSettings().maxFileSizeMB
|
||||||
|
) {
|
||||||
|
this.history.addHistoryEntry({
|
||||||
|
status: SyncStatus.ERROR,
|
||||||
|
relativePath,
|
||||||
|
message: `File size exceeds the maximum file size limit of ${this.database.getSettings().maxFileSizeMB}MB`,
|
||||||
|
type: SyncType.CREATE
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const localMetadata = this.database.getDocument(
|
const localMetadata = this.database.getDocument(
|
||||||
oldPath ?? relativePath
|
oldPath ?? relativePath
|
||||||
);
|
);
|
||||||
console.log(JSON.stringify(localMetadata));
|
|
||||||
if (!localMetadata) {
|
if (!localMetadata) {
|
||||||
if (this.database.getDocument(relativePath)) {
|
if (this.database.getDocument(relativePath)) {
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
|
|
@ -320,21 +350,13 @@ export class Syncer {
|
||||||
`Document metadata not found for ${relativePath}. This implies a corrupt local database. Consider resetting the plugin's sync history.`
|
`Document metadata not found for ${relativePath}. This implies a corrupt local database. Consider resetting the plugin's sync history.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await sleep(1000);
|
|
||||||
console.log("about to read", relativePath);
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
const contentBytes =
|
const contentBytes =
|
||||||
optimisations?.contentBytes ??
|
optimisations?.contentBytes ??
|
||||||
(await this.operations.read(relativePath));
|
(await this.operations.read(relativePath));
|
||||||
|
|
||||||
console.log("has read", relativePath);
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
let contentHash =
|
let contentHash =
|
||||||
optimisations?.contentHash ?? hash(contentBytes);
|
optimisations?.contentHash ?? hash(contentBytes);
|
||||||
console.log("has hashed", relativePath);
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
localMetadata.hash === contentHash &&
|
localMetadata.hash === contentHash &&
|
||||||
|
|
@ -349,9 +371,6 @@ export class Syncer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("about to send", relativePath);
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
const response = await this.syncService.put({
|
const response = await this.syncService.put({
|
||||||
documentId: localMetadata.documentId,
|
documentId: localMetadata.documentId,
|
||||||
parentVersionId: localMetadata.parentVersionId,
|
parentVersionId: localMetadata.parentVersionId,
|
||||||
|
|
@ -360,9 +379,6 @@ export class Syncer {
|
||||||
createdDate: updateTime
|
createdDate: updateTime
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("has sent", relativePath);
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.SUCCESS,
|
status: SyncStatus.SUCCESS,
|
||||||
source: SyncSource.PUSH,
|
source: SyncSource.PUSH,
|
||||||
|
|
@ -403,22 +419,16 @@ export class Syncer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.type === "MergingUpdate") {
|
if (response.type === "MergingUpdate") {
|
||||||
console.log(
|
|
||||||
"about to deserialize",
|
|
||||||
response.contentBase64
|
|
||||||
);
|
|
||||||
const responseBytes = deserialize(
|
const responseBytes = deserialize(
|
||||||
response.contentBase64
|
response.contentBase64
|
||||||
);
|
);
|
||||||
console.log("has deserialized", response.relativePath);
|
|
||||||
contentHash = hash(responseBytes);
|
contentHash = hash(responseBytes);
|
||||||
console.log("about to write", response.relativePath);
|
|
||||||
await this.operations.write(
|
await this.operations.write(
|
||||||
response.relativePath,
|
response.relativePath,
|
||||||
contentBytes,
|
contentBytes,
|
||||||
responseBytes
|
responseBytes
|
||||||
);
|
);
|
||||||
console.log("has written", response.relativePath);
|
|
||||||
|
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.SUCCESS,
|
status: SyncStatus.SUCCESS,
|
||||||
|
|
|
||||||
|
|
@ -252,6 +252,22 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("Maximum file size to be uploaded (MB)")
|
||||||
|
.setDesc(
|
||||||
|
"Set the maximum file size that can be uploaded to the server. Files larger than this size will be ignored."
|
||||||
|
)
|
||||||
|
.addSlider((slider) =>
|
||||||
|
slider
|
||||||
|
.setLimits(0, 32, 1)
|
||||||
|
.setDynamicTooltip()
|
||||||
|
.setInstant(false)
|
||||||
|
.setValue(this.database.getSettings().maxFileSizeMB)
|
||||||
|
.onChange(async (value) =>
|
||||||
|
this.database.setSetting("maxFileSizeMB", value)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName("Enable sync")
|
.setName("Enable sync")
|
||||||
.setDesc(
|
.setDesc(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue