feat: fetch obj segments

This commit is contained in:
Michal Szczepanski 2023-09-22 23:50:59 +02:00
parent 726ec89038
commit 0bb146555b
17 changed files with 173 additions and 31 deletions

@ -71,18 +71,21 @@ const fetchObjects = async (idList: number[], index: number, href?: string): Pro
};
export const ObjListComponent: FunctionComponent<Props> = (props) => {
const [reRender, setReRender] = useState(false);
const [objList, setObjList] = useState<ObjDto<ObjPageDataDto>[]>([]);
const [index, setIndex] = useState<number>(0);
useEffect(() => {
refetch();
}, [props]);
const refetch = () => {
fetchObjects(props.idList, index, props.href)
.then((data) => {
setObjList(data.objs);
setIndex(data.index);
})
.catch(() => LogManager.log('error'));
}, [props]);
};
const handlePinRemove = async (data: ObjDto<ObjPinDto>) => {
await new PinRemoveCommand(data.id, data.data.data.url, data.data.data.iframe).execute();
@ -109,7 +112,7 @@ export const ObjListComponent: FunctionComponent<Props> = (props) => {
for (let i = 0; i < props.idList.length; i++) {
if (props.idList[i] === id) {
props.idList.splice(i, 1);
setReRender(!reRender);
refetch();
break;
}
}

@ -23,7 +23,6 @@ import { ObjListComponent } from './obj-list.component';
import { ObjPageNoteDto } from '../../../common/model/obj/obj-note.dto';
import { PopupActiveTabStore } from '../../store/popup-active-tab.store';
import { TinyDispatcher } from '@pinmenote/tiny-dispatcher';
import Typography from '@mui/material/Typography';
interface Props {
editNoteCallback: (obj: ObjDto<ObjPageNoteDto>) => void;

@ -38,3 +38,25 @@ export interface ObjSingleChange {
export interface ObjChangesResponse {
data: ObjSingleChange[];
}
export enum SyncHashType {
PageSnapshotDataDto = '1',
PageSnapshotInfoDto = '2',
IFrame = '3',
Img = '4',
Css = '5',
Snapshot = '6',
ObjPdfDataDto = '7',
ObjPdf = '8',
ObjPageNoteDto = '9'
}
export interface SegmentSingleHash {
hash: string;
type: SyncHashType;
}
export interface SegmentHashListResponse {
hash: string;
children: SegmentSingleHash[];
}

@ -15,10 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ApiCallBase } from '../../api-call.base';
import { BeginTxResponse } from '../api-store.model';
import { BeginTxResponse, SyncHashType } from '../api-store.model';
import { FetchService } from '@pinmenote/fetch-service';
import { ICommand } from '../../../../../common/model/shared/common.dto';
import { SyncHashType } from '../../../sync/sync.model';
import { deflate } from 'pako';
export interface FileDataDto {

@ -0,0 +1,45 @@
/*
* This file is part of the pinmenote-extension distribution (https://github.com/pinmenote/pinmenote-extension).
* Copyright (c) 2023 Michal Szczepanski.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ApiCallBase } from '../../api-call.base';
import { FetchService } from '@pinmenote/fetch-service';
import { ICommand } from '../../../../../common/model/shared/common.dto';
import { SegmentHashListResponse } from '../api-store.model';
import { fnConsoleLog } from '../../../../../common/fn/fn-console';
export class ApiSegmentGetChildrenCommand
extends ApiCallBase
implements ICommand<Promise<SegmentHashListResponse | undefined>>
{
constructor(private hash: string) {
super();
}
async execute(): Promise<SegmentHashListResponse | undefined> {
await this.initTokenData();
if (!this.storeUrl) return;
try {
const resp = await FetchService.fetch<SegmentHashListResponse>(
`${this.storeUrl}/api/v1/segment/children/${this.hash}`,
{ headers: this.getAuthHeaders() },
this.refreshParams()
);
fnConsoleLog('ApiSegmentGetChildrenCommand->response', resp);
return resp.data;
} catch (e) {
fnConsoleLog('ApiSegmentGetChildrenCommand->Error', e);
}
}
}

@ -0,0 +1,41 @@
/*
* This file is part of the pinmenote-extension distribution (https://github.com/pinmenote/pinmenote-extension).
* Copyright (c) 2023 Michal Szczepanski.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ApiCallBase } from '../../api-call.base';
import { FetchService } from '@pinmenote/fetch-service';
import { ICommand } from '../../../../../common/model/shared/common.dto';
import { ObjSingleChange, SegmentHashListResponse } from '../api-store.model';
import { fnConsoleLog } from '../../../../../common/fn/fn-console';
export class ApiSegmentGetCommand extends ApiCallBase implements ICommand<Promise<Blob | undefined>> {
constructor(private hash: string) {
super();
}
async execute(): Promise<Blob | undefined> {
await this.initTokenData();
if (!this.storeUrl) return;
try {
const resp = await FetchService.fetch<Blob>(
`${this.storeUrl}/api/v1/segment/${this.hash}`,
{ headers: this.getAuthHeaders(), type: 'BLOB' },
this.refreshParams()
);
return resp.data;
} catch (e) {
fnConsoleLog('ApiSegmentGetCommand->Error', e);
}
}
}

@ -0,0 +1,44 @@
/*
* This file is part of the pinmenote-extension distribution (https://github.com/pinmenote/pinmenote-extension).
* Copyright (c) 2023 Michal Szczepanski.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ICommand } from '../../../../common/model/shared/common.dto';
import { ApiSegmentGetChildrenCommand } from '../../api/store/segment/api-segment-get-children.command';
import { ObjSingleChange, SyncHashType } from '../../api/store/api-store.model';
import { ApiSegmentGetCommand } from '../../api/store/segment/api-segment-get.command';
import { inflate } from 'pako';
import { UrlFactory } from '../../../../common/factory/url.factory';
export class SyncObjIncomingHashCommand implements ICommand<Promise<void>> {
constructor(private change: ObjSingleChange) {}
async execute(): Promise<void> {
const hashList = await new ApiSegmentGetChildrenCommand(this.change.hash).execute();
if (!hashList) return;
for (const child of hashList.children) {
const data = await new ApiSegmentGetCommand(child.hash).execute();
if (!data) continue;
if (child.type.toString() === SyncHashType.Img) {
const img = await UrlFactory.toDataUri(data);
console.log('img', img.length);
} else {
const buffer = await data.arrayBuffer();
const bytes = inflate(new Uint8Array(buffer));
const segment = new TextDecoder().decode(bytes);
console.log('ooo', JSON.parse(segment), child.type);
}
}
console.log('hashList', hashList);
}
}

@ -19,9 +19,9 @@ import { ICommand } from '../../../../common/model/shared/common.dto';
import { ObjDto } from '../../../../common/model/obj/obj.dto';
import { ObjPageNoteDto } from '../../../../common/model/obj/obj-note.dto';
import { SyncObjectCommand } from './sync-object.command';
import { SyncHashType, SyncObjectStatus } from '../sync.model';
import { SyncObjectStatus } from '../sync.model';
import { fnConsoleLog } from '../../../../common/fn/fn-console';
import { BeginTxResponse } from '../../api/store/api-store.model';
import { BeginTxResponse, SyncHashType } from '../../api/store/api-store.model';
import { ObjectStoreKeys } from '../../../../common/keys/object.store.keys';
import { ApiSegmentAddCommand } from '../../api/store/segment/api-segment-add.command';

@ -18,9 +18,9 @@ import { ICommand } from '../../../../common/model/shared/common.dto';
import { ObjDto } from '../../../../common/model/obj/obj.dto';
import { ObjPdfDataDto, ObjPdfDto } from '../../../../common/model/obj/obj-pdf.dto';
import { SyncObjectCommand } from './sync-object.command';
import { SyncHashType, SyncObjectStatus } from '../sync.model';
import { SyncObjectStatus } from '../sync.model';
import { fnConsoleLog } from '../../../../common/fn/fn-console';
import { BeginTxResponse } from '../../api/store/api-store.model';
import { BeginTxResponse, SyncHashType } from '../../api/store/api-store.model';
import { ObjectStoreKeys } from '../../../../common/keys/object.store.keys';
import { BrowserStorage } from '@pinmenote/browser-api';
import { fnB64toBlob } from '../../../../common/fn/fn-b64-to-blob';

@ -20,9 +20,9 @@ import { ObjDto } from '../../../../common/model/obj/obj.dto';
import { ObjPageDto } from '../../../../common/model/obj/obj-page.dto';
import { PageSegmentGetCommand } from '../../../../common/command/snapshot/segment/page-segment-get.command';
import { SyncObjectCommand } from './sync-object.command';
import { SyncHashType, SyncObjectStatus } from '../sync.model';
import { SyncObjectStatus } from '../sync.model';
import { fnConsoleLog } from '../../../../common/fn/fn-console';
import { BeginTxResponse } from '../../api/store/api-store.model';
import { BeginTxResponse, SyncHashType } from '../../api/store/api-store.model';
import { PageSnapshotDto } from '../../../../common/model/obj/page-snapshot.dto';
import { ApiSegmentAddCommand } from '../../api/store/segment/api-segment-add.command';
import { fnB64toBlob } from '../../../../common/fn/fn-b64-to-blob';

@ -22,13 +22,13 @@ import { ObjGetCommand } from '../../../common/command/obj/obj-get.command';
import { ObjPageDto } from '../../../common/model/obj/obj-page.dto';
import { ObjPdfDto } from '../../../common/model/obj/obj-pdf.dto';
import { ObjPinDto } from '../../../common/model/obj/obj-pin.dto';
import { SyncNoteCommand } from './obj/sync-note.command';
import { SyncPageNoteCommand } from './obj/sync-page-note.command';
import { SyncPdfCommand } from './obj/sync-pdf.command';
import { SyncPinCommand } from './obj/sync-pin.command';
import { SyncNoteCommand } from './outgoing/sync-note.command';
import { SyncPageNoteCommand } from './outgoing/sync-page-note.command';
import { SyncPdfCommand } from './outgoing/sync-pdf.command';
import { SyncPinCommand } from './outgoing/sync-pin.command';
import { SyncObjectStatus, SyncProgress } from './sync.model';
import { SyncRemovedCommand } from './obj/sync-removed.command';
import { SyncSnapshotCommand } from './obj/sync-snapshot.command';
import { SyncRemovedCommand } from './outgoing/sync-removed.command';
import { SyncSnapshotCommand } from './outgoing/sync-snapshot.command';
import { fnConsoleLog } from '../../../common/fn/fn-console';
import { fnSleep } from '../../../common/fn/fn-sleep';
import { BeginTxResponse } from '../api/store/api-store.model';

@ -26,6 +26,7 @@ import { TokenStorageGetCommand } from '../../../common/command/server/token/tok
import jwtDecode from 'jwt-decode';
import { TokenDataDto } from '../../../common/model/shared/token.dto';
import { ApiObjGetChangesCommand } from '../api/store/obj/api-obj-get-changes.command';
import { SyncObjIncomingHashCommand } from './incoming/sync-obj-incoming-hash.command';
export class SyncServerCommand implements ICommand<Promise<void>> {
private static isInSync = false;
@ -57,7 +58,7 @@ export class SyncServerCommand implements ICommand<Promise<void>> {
const changesResp = await new ApiObjGetChangesCommand().execute();
if (!changesResp) return;
for (const change of changesResp.data) {
console.log(change);
await new SyncObjIncomingHashCommand(change).execute();
break;
}
const token = await new TokenStorageGetCommand().execute();

@ -29,15 +29,3 @@ export enum SyncObjectStatus {
EMPTY_LIST,
LAST_ELEMENT
}
export enum SyncHashType {
PageSnapshotDataDto = '1',
PageSnapshotInfoDto = '2',
IFrame = '3',
Img = '4',
Css = '5',
Snapshot = '6',
ObjPdfDataDto = '7',
ObjPdf = '8',
ObjPageNoteDto = '9'
}