Skip to content

Commit

Permalink
Fix opening files from favorites and recent
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Aug 8, 2023
1 parent 34fe83c commit 45fe74d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
39 changes: 29 additions & 10 deletions application/client/src/app/ui/elements/favorites/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EntityType, getFileName } from '@platform/types/files';
import { notifications, Notification } from '@ui/service/notifications';
import { createPassiveMatcheeList } from '@module/matcher';
import { unique } from '@platform/env/sequence';
import { getFileTypeByFilename } from '@platform/types/observe/types/file';

import * as Factory from '@platform/types/observe/factory';

Expand Down Expand Up @@ -158,7 +159,7 @@ export class State extends Holder {
this.ilc
.ilc()
.services.system.session.initialize()
.configure(
.observe(
new Factory.File()
.asText()
.type(Factory.FileType.Text)
Expand All @@ -170,14 +171,32 @@ export class State extends Holder {
});
},
auto: (): void => {
// TODO: needs implementation
// this.ilc
// .ilc()
// .services.system.opener.text(item.filename)
// .auto()
// .catch((err: Error) => {
// this.ilc.log().error(`Fail to open text file; error: ${err.message}`);
// });
const filetype = getFileTypeByFilename(item.filename);
if (filetype === Factory.FileType.Text) {
this.ilc
.ilc()
.services.system.session.initialize()
.observe(
new Factory.File().asText().type(filetype).file(item.filename).get(),
)
.catch((err: Error) => {
this.ilc.log().error(`Fail to open text file; error: ${err.message}`);
});
} else {
this.ilc
.ilc()
.services.system.session.initialize()
.configure(
new Factory.File()
.type(filetype)
.file(item.filename)
.guessParser()
.get(),
)
.catch((err: Error) => {
this.ilc.log().error(`Fail to open text file; error: ${err.message}`);
});
}
},
};
}
Expand Down Expand Up @@ -268,7 +287,7 @@ export class State extends Holder {
const data = await favorites.places().get();
this.roots = data.filter((f) => f.exists).map((f) => f.path);
await this.includeFromFolder(this.roots).catch((err: Error) => {
console.log(`Fail to get items from folder: ${err.message}`);
this.ilc.log().error(`Fail to get items from folder: ${err.message}`);
});
this.scanning = undefined;
this.update.emit();
Expand Down
4 changes: 4 additions & 0 deletions application/client/src/app/ui/elements/filter/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export class Filter {
enter: Subject<string>;
focus: Subject<void>;
blur: Subject<void>;
up: Subject<void>;
down: Subject<void>;
}> = new Subjects({
change: new Subject<string>(),
drop: new Subject<void>(),
enter: new Subject<string>(),
focus: new Subject<void>(),
blur: new Subject<void>(),
up: new Subject<void>(),
down: new Subject<void>(),
});
private _element!: HTMLInputElement | undefined;

Expand Down
13 changes: 12 additions & 1 deletion application/client/src/app/ui/elements/filter/input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { UntypedFormControl } from '@angular/forms';
import { Filter } from './filter';

import * as dom from '@ui/env/dom';

export class FilterInput {
public control: UntypedFormControl = new UntypedFormControl();
protected filter: Filter;
Expand All @@ -23,7 +25,16 @@ export class FilterInput {
this.drop();
return true;
}

if (event.key === 'ArrowUp') {
this.filter.subjects.get().up.emit();
dom.stop(event);
return false;
}
if (event.key === 'ArrowDown') {
this.filter.subjects.get().down.emit();
dom.stop(event);
return false;
}
this.filter.subjects.get().change.emit(this._safe());
return true;
}
Expand Down
1 change: 0 additions & 1 deletion application/holder/src/env/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export function getFileEntity(filename: string): File | undefined | Error {
}

export function detectSupportedFileType(filename: string): FileType {
// TODO: detect binary or text file correctly
switch (path.extname(filename).toLowerCase()) {
case '.dlt':
return FileType.Binary;
Expand Down
4 changes: 4 additions & 0 deletions application/platform/env/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ export class Subject<T> {
return this as unknown as Subject<C>;
}

public isAlone(): boolean {
return this._handlers.length === 0;
}

private _unsubscribe(id: string) {
let index: number = -1;
this._handlers.forEach((handle: any, i: number) => {
Expand Down
13 changes: 13 additions & 0 deletions application/platform/types/observe/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ class Factory<T> {
return this as unknown as T;
}

public guessParser(): T {
const parsers = this.observe.origin.getSupportedParsers();
if (parsers.length === 0) {
throw new Error(
`Origin "${this.observe.origin.getNatureAlias()}" doesn't have any suitable parseres.`,
);
}
const Ref = parsers[0];
this.observe.parser.change(new Ref(Ref.initial(), undefined));
this.updated().parser();
return this as unknown as T;
}

public get(): $.Observe {
const parsers = this.observe.origin.getSupportedParsers().map((ref) => ref.alias());
const selected = this.observe.parser.alias();
Expand Down
19 changes: 19 additions & 0 deletions application/platform/types/observe/types/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@ export function getFileTypeFrom(smth: unknown): FileType | Error {
}
}

export function extname(filename: string): string {
const matches = filename.match(/\.[\w\d_]*$/gi);
if (matches !== null) {
return matches[0];
}
return '';
}

export function getFileTypeByFilename(filename: string): FileType {
switch (extname(filename).toLowerCase()) {
case '.dlt':
return FileType.Binary;
case '.pcapng':
return FileType.PcapNG;
default:
return FileType.Text;
}
}

export type FileName = string;

0 comments on commit 45fe74d

Please sign in to comment.