Skip to content

Commit

Permalink
implement filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault committed Mar 11, 2024
1 parent 45649af commit 117d7c8
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuclia-sync-agent-app",
"version": "1.0.11",
"version": "1.1.0",
"description": "\"This is a Nuclia Sync Agent App\"",
"main": "build/index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.0 (2024-03-11)

- Support filtering options

# 1.0.2 (2024-02-26)

- Fix npm package
Expand Down
1 change: 1 addition & 0 deletions server/src/logic/connector/domain/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SyncItemValidator = z.object({
metadata: z.record(z.string()),
status: z.nativeEnum(FileStatus).optional(),
modifiedGMT: z.string().optional(),
mimeType: z.string().optional(),
isFolder: z.boolean().optional(),
parents: z.array(z.string()).optional(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import { forkJoin, map, Observable, of, switchMap } from 'rxjs';
import { ConnectorParameters, FileStatus, IConnector, Link, SearchResults, SyncItem } from '../../domain/connector';
import { SourceConnectorDefinition } from '../factory';
import { lookup } from 'mime-types';

const FILES_TO_IGNORE = ['.DS_Store', 'Thumbs.db'];

Expand Down Expand Up @@ -145,6 +146,7 @@ class FolderImpl implements IConnector {
return files.map((file) => ({
title: file.split('/').pop() || '',
originalId: file,
mimeType: lookup(file) || 'application/octet-stream',
metadata: {
path: file.split('/').slice(0, -1).join('/'),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ export class GDriveImpl extends OAuthBaseConnector implements IConnector {
originalId: item.id,
modifiedGMT: item.modifiedTime,
parents: item.parents,
mimeType: needsPdfConversion ? 'application/pdf' : item.mimeType,
metadata: {
needsPdfConversion: needsPdfConversion ? 'yes' : 'no',
mimeType: needsPdfConversion ? 'application/pdf' : item.mimeType,
},
status: FileStatus.PENDING,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ export class OneDriveImpl extends OAuthBaseConnector implements IConnector {
title: item.name,
originalId: item.id,
modifiedGMT: item.lastModifiedDateTime,
mimeType: item.file.mimeType,
metadata: {
mimeType: item.file.mimeType,
downloadLink: item['@microsoft.graph.downloadUrl'],
path: item.parentReference.path,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class SitemapImpl implements IConnector {
status: FileStatus.PENDING,
uuid: `${new Date().getTime()}`,
originalId: parsedUrl.loc,
mimeType: 'text/html',
metadata: {
uri: parsedUrl.loc,
path: parsedUrl.loc.replace(/https?:\/\//, ''),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const data = [
title: 'PO6300590983',
originalId: '1v8WV_aNM5qB_642saVlPhOkN1xI0NtQo',
modifiedGMT: '2023-11-29T12:49:27.539Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -20,9 +20,9 @@ const data = [
title: 'PO6300604892',
originalId: '19QJOCaOY4R8EQZ7VDrmmu2FBkeOlRAxJ',
modifiedGMT: '2023-11-27T12:48:06.061Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -31,9 +31,9 @@ const data = [
title: 'PO4550970006',
originalId: '1-5mIXJuiLTFxTO4mmVdXGNdf-Da-EzgA',
modifiedGMT: '2023-11-27T12:46:08.712Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand Down Expand Up @@ -145,9 +145,9 @@ describe('Test last modified', () => {
title: 'PO6300590983',
originalId: '1v8WV_aNM5qB_642saVlPhOkN1xI0NtQo',
modifiedGMT: '2023-11-29T12:49:27.539Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
path: '',
},
status: FileStatus.PENDING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const data = [
title: 'PO6300590983',
originalId: '1v8WV_aNM5qB_642saVlPhOkN1xI0NtQo',
modifiedGMT: '2023-11-29T12:49:27.539Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -20,9 +20,9 @@ const data = [
title: 'PO6300604892',
originalId: '19QJOCaOY4R8EQZ7VDrmmu2FBkeOlRAxJ',
modifiedGMT: '2023-11-27T12:48:06.061Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -31,9 +31,9 @@ const data = [
title: 'PO4550970006',
originalId: '1-5mIXJuiLTFxTO4mmVdXGNdf-Da-EzgA',
modifiedGMT: '2023-11-27T12:46:08.712Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand Down Expand Up @@ -145,9 +145,9 @@ describe('Test last modified', () => {
title: 'PO6300590983',
originalId: '1v8WV_aNM5qB_642saVlPhOkN1xI0NtQo',
modifiedGMT: '2023-11-29T12:49:27.539Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: FileStatus.PENDING,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const data = [
title: 'PO6300590983',
originalId: '1v8WV_aNM5qB_642saVlPhOkN1xI0NtQo',
modifiedGMT: '2023-11-29T12:49:27.539Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -20,9 +20,9 @@ const data = [
title: 'PO6300604892',
originalId: '19QJOCaOY4R8EQZ7VDrmmu2FBkeOlRAxJ',
modifiedGMT: '2023-11-27T12:48:06.061Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand All @@ -31,9 +31,9 @@ const data = [
title: 'PO4550970006',
originalId: '1-5mIXJuiLTFxTO4mmVdXGNdf-Da-EzgA',
modifiedGMT: '2023-11-27T12:46:08.712Z',
mimeType: 'application/pdf',
metadata: {
needsPdfConversion: 'yes',
mimeType: 'application/pdf',
},
status: 'PENDING',
},
Expand Down
4 changes: 2 additions & 2 deletions server/src/logic/sync/domain/nuclia-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class NucliaCloud {
upload(
originalId: string,
filename: string,
data: { buffer?: ArrayBuffer; text?: TextField; metadata?: any },
data: { buffer?: ArrayBuffer; text?: TextField; metadata?: any; mimeType?: string },
): Observable<{ success: boolean; message?: string }> {
const slug = sha256(originalId);
const text = data.text;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class NucliaCloud {
try {
return resource
.upload('file', buffer, false, {
contentType: data.metadata.mimeType || lookup(filename) || 'application/octet-stream',
contentType: data.mimeType || lookup(filename) || 'application/octet-stream',
filename,
})
.pipe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { concatMap, delay, lastValueFrom, map, of, switchMap, tap, toArray } from 'rxjs';
import { concatMap, delay, filter, lastValueFrom, map, of, switchMap, tap, toArray } from 'rxjs';

import { EVENTS } from '../../../../events/events';
import { eventEmitter } from '../../../../server';
Expand All @@ -10,6 +10,7 @@ import { ISyncRepository } from '../sync.repository';
import { RefreshAccessToken } from './refresh-access-token.use-case';
import { SyncSingleFile } from './sync-single-file.use-case';
import { UpdateSync } from './update-sync.use-case';
import { lookup } from 'mime-types';

require('localstorage-polyfill');

Expand Down Expand Up @@ -66,7 +67,32 @@ export class SyncAllFolders implements SyncAllFoldersUseCase {
}

processItems(syncEntity: SyncEntity, items: SyncItem[]) {
const filteredMimetypes = (syncEntity.filters?.fileExtensions?.extensions || '')
.split(',')
.map((ext) => lookup(ext.trim()) || '')
.filter((ext) => ext);
return of(...items).pipe(
filter((item) => {
let isExtensionOk = true;
if (!item.mimeType) {
isExtensionOk = false;
} else {
if (filteredMimetypes.length > 0) {
const isFiltered = filteredMimetypes.includes(item.mimeType);
isExtensionOk = syncEntity.filters?.fileExtensions?.exclude ? !isFiltered : isFiltered;
}
}
let isDateOk = true;
if (item.modifiedGMT && syncEntity.filters?.modified) {
if (syncEntity.filters.modified.from) {
isDateOk = item.modifiedGMT >= syncEntity.filters.modified.from;
}
if (syncEntity.filters.modified.to) {
isDateOk = isDateOk && item.modifiedGMT <= syncEntity.filters.modified.to;
}
}
return isDateOk && isExtensionOk;
}),
concatMap((item) =>
new SyncSingleFile(syncEntity, item).execute().pipe(
map((res) => ({ id: item.originalId, success: res.success })),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class SyncSingleFile implements SyncSingleFileUseCase {
return nucliaConnector.upload(item.originalId, item.title, {
buffer: arrayBuffer,
metadata: { ...item.metadata, labels: sync.labels },
mimeType: item.mimeType,
});
}),
);
Expand Down

0 comments on commit 117d7c8

Please sign in to comment.