Skip to content

Commit

Permalink
Apply file size restriction in desktop (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault authored Mar 20, 2023
1 parent 6cfb308 commit 2578a71
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
15 changes: 9 additions & 6 deletions apps/desktop/src/app/history/history.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ <h2>{{ 'history.title' | translate }}</h2>
<nsi-back-button link="/history">{{ 'history.back' | translate }}</nsi-back-button>
</header>

<pa-table [columns]="(showActive | async) ? 'repeat(4, 1fr) 200px 1fr' : 'repeat(5, 1fr)'">
<pa-table columns="repeat(4, 1fr) 200px 1fr">
<pa-table-header>
<pa-table-cell header>{{ 'history.date' | translate }}</pa-table-cell>
<pa-table-cell header>{{ 'history.source' | translate }}</pa-table-cell>
<pa-table-cell header>{{ 'upload.steps.destination' | translate }}</pa-table-cell>
<pa-table-cell header>{{ 'history.knowledge_box' | translate }}</pa-table-cell>
<pa-table-cell
*ngIf="showActive | async"
header></pa-table-cell>
<pa-table-cell header></pa-table-cell>
<pa-table-cell header>{{ 'history.data' | translate }}</pa-table-cell>
</pa-table-header>
<pa-table-row *ngFor="let upload of uploads | async">
<pa-table-cell>{{ upload.date }}</pa-table-cell>
<pa-table-cell>{{ upload.from }}</pa-table-cell>
<pa-table-cell>{{ upload.to }}</pa-table-cell>
<pa-table-cell>{{ upload.kbSlug }}</pa-table-cell>
<pa-table-cell *ngIf="showActive | async">
<pa-table-cell>
<stf-progress-bar
*ngIf="!upload.completed"
[progress]="upload.progress"></stf-progress-bar>
<pa-icon
*ngIf="upload.completed"
*ngIf="upload.completed && !upload.errors"
name="check"
color="#f06"></pa-icon>
<pa-icon
*ngIf="upload.completed && upload.errors"
name="circle-cross"
color="#f06"
[paTooltip]="upload.errors"></pa-icon>
</pa-table-cell>
<pa-table-cell>{{ upload.total }}</pa-table-cell>
</pa-table-row>
Expand Down
4 changes: 4 additions & 0 deletions apps/desktop/src/app/history/history.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class HistoryComponent {
progress: (100 * sync.files.filter((f) => f.status === FileStatus.UPLOADED).length) / sync.files.length,
started: sync.started,
completed: sync.completed,
errors: sync.files
.filter((f) => f.status === FileStatus.ERROR)
.map((f) => f.error || 'Unknown error')
.join(' – '),
})),
),
);
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/app/history/history.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ProgressBarModule } from '@flaps/common';
import { TranslateModule } from '@ngx-translate/core';

import { HistoryComponent } from './history.component';
import { PaButtonModule, PaIconModule, PaTableModule } from '@guillotinaweb/pastanaga-angular';
import { PaButtonModule, PaIconModule, PaTableModule, PaTooltipModule } from '@guillotinaweb/pastanaga-angular';
import { BackButtonComponent } from '@nuclia/sistema';

@NgModule({
Expand All @@ -18,6 +18,7 @@ import { BackButtonComponent } from '@nuclia/sistema';
PaButtonModule,
PaIconModule,
PaTableModule,
PaTooltipModule,
],
exports: [],
declarations: [HistoryComponent],
Expand Down
54 changes: 42 additions & 12 deletions apps/desktop/src/app/sync/destinations/nuclia-cloud.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INuclia, Nuclia, NucliaOptions, WritableKnowledgeBox } from '@nuclia/core';
import { catchError, from, map, Observable, of, switchMap } from 'rxjs';
import { Account, INuclia, Nuclia, NucliaOptions, WritableKnowledgeBox } from '@nuclia/core';
import { catchError, from, map, Observable, of, switchMap, tap } from 'rxjs';
import {
ConnectorParameters,
ConnectorSettings,
Expand All @@ -25,7 +25,8 @@ export const NucliaCloudKB: DestinationConnectorDefinition = {

class NucliaCloudKBImpl implements IDestinationConnector {
nuclia: INuclia;
kb?: WritableKnowledgeBox;
kbs: { [slug: string]: WritableKnowledgeBox } = {};
account?: Account;

constructor(nuclia: INuclia) {
this.nuclia = nuclia;
Expand Down Expand Up @@ -56,10 +57,18 @@ class NucliaCloudKBImpl implements IDestinationConnector {
): Observable<void> {
if (params && params['kb'] && data.blob) {
const blob = data.blob;
const kb$ = this.kb
? of(this.kb)
: this.nuclia.db.getKnowledgeBox(localStorage.getItem(ACCOUNT_KEY) || '', params['kb']);
return kb$.pipe(
const mimetype = lookup(filename) || 'application/octet-stream';
return this.getAccount().pipe(
tap((account) => {
const applicableLimit = this.isMedia(mimetype)
? account.limits.upload.upload_limit_max_media_file_size
: account.limits.upload.upload_limit_max_non_media_file_size;
if (blob.size > applicableLimit) {
console.error(`File too large. Size=${blob.size}, limit=${applicableLimit}`);
throw new Error(`File "${filename}" is too large.`);
}
}),
switchMap(() => this.getKb(params['kb'])),
switchMap((kb) =>
from(sha256(originalId)).pipe(
switchMap((slug) =>
Expand All @@ -77,7 +86,7 @@ class NucliaCloudKBImpl implements IDestinationConnector {
),
switchMap((resource) => {
return resource.upload('file', new File([blob], filename), false, {
contentType: lookup(filename) || 'application/octet-stream',
contentType: mimetype,
});
}),
),
Expand All @@ -95,10 +104,7 @@ class NucliaCloudKBImpl implements IDestinationConnector {
data: { uri: string; extra_headers: { [key: string]: string } },
): Observable<void> {
if (params && params['kb']) {
const kb$ = this.kb
? of(this.kb)
: this.nuclia.db.getKnowledgeBox(localStorage.getItem(ACCOUNT_KEY) || '', params['kb']);
return kb$.pipe(
return this.getKb(params['kb']).pipe(
switchMap((kb) => kb.createResource({ title: filename, files: { [filename]: { file: data } } })),
map(() => undefined),
);
Expand Down Expand Up @@ -128,4 +134,28 @@ class NucliaCloudKBImpl implements IDestinationConnector {
})),
);
}

private getKb(slug: string): Observable<WritableKnowledgeBox> {
if (!this.kbs[slug]) {
return this.nuclia.db
.getKnowledgeBox(localStorage.getItem(ACCOUNT_KEY) || '', slug)
.pipe(tap((kb) => (this.kbs[slug] = kb)));
} else {
return of(this.kbs[slug]);
}
}

private getAccount(): Observable<Account> {
if (!this.account) {
return this.nuclia.db
.getAccount(localStorage.getItem(ACCOUNT_KEY) || '')
.pipe(tap((account) => (this.account = account)));
} else {
return of(this.account);
}
}

private isMedia(mimetype: string): boolean {
return mimetype.startsWith('image/') || mimetype.startsWith('video/') || mimetype.startsWith('audio/');
}
}
2 changes: 2 additions & 0 deletions apps/desktop/src/app/sync/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum FileStatus {
PENDING = 'PENDING',
PROCESSING = 'PROCESSING',
UPLOADED = 'UPLOADED',
ERROR = 'ERROR',
}

export interface SyncItem {
Expand All @@ -43,6 +44,7 @@ export interface SyncItem {
originalId: string;
metadata: { [key: string]: string };
status: FileStatus;
error?: string;
}

export interface SearchResults {
Expand Down
11 changes: 8 additions & 3 deletions apps/desktop/src/app/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export class SyncService {
destinationInstance.uploadLink!(f.title, sync.destination.params, link).pipe(
tap(() => {
f.status = FileStatus.UPLOADED;
sync.completed = true;
this.onQueueUpdate();
}),
),
Expand All @@ -191,9 +190,14 @@ export class SyncService {
return destinationInstance
.upload(f.originalId, f.title, sync.destination.params, { blob })
.pipe(
catchError((error) => {
f.status = FileStatus.ERROR;
f.error = error.toString();
this.onQueueUpdate();
return of(undefined);
}),
tap(() => {
f.status = FileStatus.UPLOADED;
sync.completed = true;
f.status = FileStatus.ERROR ? FileStatus.ERROR : FileStatus.UPLOADED;
this.onQueueUpdate();
}),
);
Expand All @@ -216,6 +220,7 @@ export class SyncService {
);
}),
tap(() => {
sync.completed = true;
this.onQueueUpdate();
}),
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuclia",
"version": "1.2.3",
"version": "1.2.4",
"license": "MIT",
"author": "Nuclia.cloud",
"description": "Nuclia frontend apps and libs",
Expand Down

0 comments on commit 2578a71

Please sign in to comment.