Skip to content

Commit

Permalink
update ContainersController
Browse files Browse the repository at this point in the history
  • Loading branch information
raronpxcsw committed Sep 27, 2023
1 parent 234fe41 commit 0222718
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 152 deletions.
3 changes: 2 additions & 1 deletion projects/aas-lib/src/lib/aas-tree/aas-tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
EndpointType,
selectSubmodel,
getIdShortPath,
mimeTypeToExtension,
} from 'common';

import { AASTreeRow, AASTreeFeatureState } from './aas-tree.state';
Expand All @@ -33,7 +34,7 @@ import { ShowVideoFormComponent } from './show-video-form/show-video-form.compon
import { OperationCallFormComponent } from './operation-call-form/operation-call-form.component';
import { AASTreeSearch } from './aas-tree-search';
import { AASTree } from '../types/aas-tree';
import { basename, encodeBase64Url, mimeTypeToExtension } from '../convert';
import { basename, encodeBase64Url } from '../convert';
import { AASQuery } from '../types/aas-query-params';
import { ViewQuery } from '../types/view-query-params';
import { WindowService } from '../window.service';
Expand Down
5 changes: 3 additions & 2 deletions projects/aas-lib/src/lib/aas-tree/aas-tree.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import {
selectReferable,
isBooleanType,
toLocale,
toBoolean
toBoolean,
mimeTypeToExtension
} from 'common';

import { AASTreeRow, AASTreeState, DisplayType, SearchTerm } from './aas-tree.state';
import * as AASTreeActions from './aas-tree.actions';
import { basename, normalize, mimeTypeToExtension } from '../convert';
import { basename, normalize } from '../convert';

const initialState: AASTreeState = {
rows: [],
Expand Down
97 changes: 2 additions & 95 deletions projects/aas-lib/src/lib/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,6 @@ import { TranslateService } from '@ngx-translate/core';
import { ApplicationError, ErrorData, stringFormat } from 'common';
import { noop, toString } from 'lodash-es';

const mimeTypes = new Map<string, string>(
[
['audio/aac', '.aac'],
['application/x-abiword', '.abw'],
['application/x-freearc', '.arc'],
['image/avif', '.avif'],
['video/x-msvideo', '.avi'],
['application/vnd.amazon.ebook', '.azw'],
['application/octet-stream', '.bin'],
['.bmp', 'image/bmp'],
['application/x-bzip', '.bz'],
['application/x-bzip2', '.bz2'],
['application/x-cdf', '.cda'],
['application/x-csh', '.csh'],
['text/css', '.css'],
['text/csv', '.csv'],
['application/msword', '.doc'],
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.docx'],
['application/vnd.ms-fontobject', '.eot'],
['application/epub+zip', '.epub'],
['application/gzip', '.gz'],
['image/gif', '.gif'],
['text/html', '.html'],
['image/vnd.microsoft.icon', '.ico'],
['text/calendar', '.ics'],
['application/java-archive', '.jar'],
['image/jpeg', '.jar'],
['text/javascript', '.js'],
['application/json', '.json'],
['application/ld+json', '.jsonld'],
['audio/midi', '.midi'],
['audio/x-midi', '.midi'],
['audio/mpeg', '.mp3'],
['video/mp4', '.mp4'],
['video/mpeg', '.mpeg'],
['application/vnd.apple.installer+xml', '.mpkg'],
['application/vnd.oasis.opendocument.presentation', '.odp'],
['application/vnd.oasis.opendocument.spreadsheet', '.ods'],
['application/vnd.oasis.opendocument.text', '.odt'],
['audio/ogg', '.oga'],
['video/ogg', '.ogv'],
['application/ogg', '.ogx'],
['audio/opus', '.opus'],
['font/otf', '.otf'],
['image/png', '.png'],
['application/pdf', '.pdf'],
['application/x-httpd-php', '.php'],
['application/vnd.ms-powerpoint', '.ppt'],
['application/vnd.openxmlformats-officedocument.presentationml.presentation', '.pptx'],
['application/vnd.rar', '.rar'],
['application/rtf', '.rtf'],
['application/x-sh', '.sh'],
['image/svg+xml', '.svg'],
['application/x-tar', '.tar'],
['image/tiff', '.tiff'],
['video/mp2t', '.ts'],
['font/ttf', '.ttf'],
['text/plain', '.txt'],
['application/vnd.visio', '.vsd'],
['audio/wav', '.wav'],
['audio/webm', '.weba'],
['video/webm', '.webp'],
['font/woff', '.woff'],
['font/woff2', '.woff2'],
['application/xhtml+xml', '.xhtml'],
['application/vnd.ms-excel', '.xls'],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.xlsx'],
['application/xml', '.xml'],
['text/xml', '.xml'],
['application/vnd.mozilla.xul+xml', '.xul'],
['application/zip', '.zip'],
['video/3gpp', '.3gp'],
['audio/3gpp', '.3gp'],
['video/3gpp2', '.3g2'],
['audio/3gpp2', '.3g2'],
['application/x-7z-compressed', '.7z']
]
);

/**
* Converts a message to a localized text.
* @param message The current message.
Expand Down Expand Up @@ -149,6 +70,8 @@ export async function resolveError(error: unknown, translate: TranslateService):
noop();
}
}
} else if (typeof error.error === 'string') {
message = error.error;
}
}

Expand Down Expand Up @@ -229,22 +152,6 @@ export function isBase64(s: string): boolean {
return /^[A-Za-z0-9+/]*[=]{0,2}$/.test(s);
}

/** Returns the file extension that corresponds to the specified MIME type. */
export function mimeTypeToExtension(mimeType: string): string | undefined {
return mimeTypes.get(mimeType);
}

/** Returns the MIME type that corresponds ti the specified file extension */
export function extensionToMimeType(extension: string): string | undefined {
for (const tuple of mimeTypes) {
if (tuple[1] === extension) {
return tuple[0];
}
}

return undefined;
}

/**
* Converts a Blob to a base64 encoded string.
* @param blob The current Blob.
Expand Down
26 changes: 3 additions & 23 deletions projects/aas-lib/src/test/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { TranslateService } from "@ngx-translate/core";
import { ApplicationError } from "common";
import { encodeBase64Url, basename, messageToString, normalize, decodeBase64Url, isBase64, mimeTypeToExtension, extensionToMimeType, extension, convertBlobToBase64Async } from "../lib/convert";
import { encodeBase64Url, basename, messageToString, normalize, decodeBase64Url, isBase64, extension, convertBlobToBase64Async } from "../lib/convert";

describe('convert', function () {
describe('basename', function () {
Expand Down Expand Up @@ -76,28 +76,8 @@ describe('convert', function () {
});
});

describe('mimeTypeToExtension', function () {
it('return ".png" for "image/png"', function () {
expect(mimeTypeToExtension('image/png')).toEqual('.png');
});

it('return undefined for an unknown MIME type', function () {
expect(mimeTypeToExtension('unknown')).toBeUndefined();
});

describe('extensionToMimeType', function () {
it('return "image/png" for ".png"', function () {
expect(extensionToMimeType('.png')).toEqual('image/png');
});

it('return undefined for an unknown MIME type', function () {
expect(extensionToMimeType('unknown')).toBeUndefined();
});
});
});

describe('convertBlobToBase64Async', function() {
it('converts the Blob content to a base64 encoded string', async function() {
describe('convertBlobToBase64Async', function () {
it('converts the Blob content to a base64 encoded string', async function () {
const blob = new Blob(['Hello World!']);
await expectAsync(convertBlobToBase64Async(blob)).toBeResolvedTo('SGVsbG8gV29ybGQh');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import { Component } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { TranslateService } from '@ngx-translate/core';
import { convertBlobToBase64Async, extension, extensionToMimeType } from 'aas-lib';
import { aas, toInvariant, toLocale } from 'common';
import { noop } from 'lodash-es';
import { convertBlobToBase64Async, extension } from 'aas-lib';
import { aas, extensionToMimeType, toInvariant, toLocale } from 'common';

export interface LangStringRow extends aas.LangString {
index: number;
Expand Down Expand Up @@ -169,7 +168,7 @@ export class EditElementFormComponent {
this.contentType = extensionToMimeType(ext);
convertBlobToBase64Async(files[0]).then(
value => this.value = value
).catch(error => {
).catch(() => {
this.value = temp;
this.pushMessage('Unable to read file.');
})
Expand Down
6 changes: 3 additions & 3 deletions projects/aas-server/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ export class App {
}

return res.status(500).json({
message: "Internal Server Error",
message: err.message
});
}

if (err instanceof Error) {
return res.status(500).json({
message: "Internal Server Error",
message: err.message
});
}

Expand All @@ -113,7 +113,7 @@ export class App {

private notFoundHandler = (_req: Request, res: Response) => {
res.status(404).send({
message: "Not Found",
message: "Not Found"
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ export class ContainersController extends ControllerBase {
): Promise<string[]> {
try {
this.logger.start('updateDocument');
const env: aas.Environment = JSON.parse((await fs.promises.readFile(content.path)).toString());
const buffer = await fs.promises.readFile(content.path);
const env: aas.Environment = JSON.parse(buffer.toString());
return await this.aasProvider.updateDocumentAsync(decodeBase64Url(url), decodeBase64Url(id), env);
} finally {
this.logger.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ export class AasxServerPackage extends AASPackage {
return document;
}

public async commitDocumentAsync(source: AASDocument, content: aas.Environment): Promise<string[]> {
public async commitDocumentAsync(target: AASDocument, content: aas.Environment): Promise<string[]> {
let messages: string[] | undefined;
if (source.content && content) {
const diffs = await diffAsync(source.content, content);
if (target.content && content) {
const diffs = await diffAsync(content, target.content);
if (diffs.length > 0) {
messages = await this.source.commitAsync(source.content, content, diffs);
messages = await this.source.commitAsync(content, target.content, diffs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ export class AasxServerV3 extends AasxServer {

public async commitAsync(
source: aas.Environment,
destination: aas.Environment,
target: aas.Environment,
diffs: DifferenceItem[]): Promise<string[]> {
const messages: string[] = [];
const aasId = encodeBase64Url(destination.assetAdministrationShells[0].id);
const aasId = encodeBase64Url(target.assetAdministrationShells[0].id);
for (const diff of diffs) {
if (diff.type === 'inserted') {
if (isSubmodel(diff.sourceElement)) {
messages.push(await this.postSubmodelAsync(aasId, diff.sourceElement));
} else if (isSubmodelElement(diff.sourceElement)) {
const submodel = this.getSubmodel(destination, diff.destinationParent);
const submodel = this.getSubmodel(target, diff.destinationParent);
messages.push(await this.postSubmodelElementAsync(submodel, diff.sourceElement));
} else {
throw new Error(`Inserting "${diff?.sourceElement?.modelType}" is not implemented.`);
Expand All @@ -129,7 +129,7 @@ export class AasxServerV3 extends AasxServer {
if (isSubmodel(diff.sourceElement)) {
messages.push(await this.putSubmodelAsync(aasId, diff.sourceElement));
} else if (isSubmodelElement(diff.sourceElement)) {
const submodel = this.getSubmodel(destination, diff.destinationElement);
const submodel = this.getSubmodel(target, diff.destinationElement);
messages.push(await this.putSubmodelElementAsync(submodel, diff.sourceElement as aas.SubmodelElement));
} else if (isAssetAdministrationShell(diff.sourceElement)) {
messages.push(await this.putShellAsync(diff.sourceElement));
Expand All @@ -140,7 +140,7 @@ export class AasxServerV3 extends AasxServer {
if (isSubmodel(diff.destinationElement)) {
messages.push(await this.deleteSubmodelAsync(diff.destinationElement.id));
} else if (isSubmodelElement(diff.destinationElement)) {
const submodel = this.getSubmodel(destination, diff.destinationParent);
const submodel = this.getSubmodel(target, diff.destinationParent);
messages.push(await this.deleteSubmodelElementAsync(submodel, diff.destinationElement));
} else {
throw new Error(`Deleting "${diff?.destinationElement?.modelType}" is not implemented.`);
Expand Down
15 changes: 10 additions & 5 deletions projects/aas-server/src/app/packages/json-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*
*****************************************************************************/

import { aas } from 'common';
import path from 'path';
import { aas, extensionToMimeType } from 'common';
import { AASWriter } from './aas-writer.js';

/** */
Expand Down Expand Up @@ -349,13 +350,17 @@ export class JsonWriter extends AASWriter {
}

private writeFile(source: aas.File): aas.File {
if (!source.contentType) {
throw new Error('Blob.contentType');
let contentType: string | undefined = source.contentType;
if (source.value && !contentType) {
contentType = extensionToMimeType(path.extname(source.value.trim()));
if (!contentType) {
throw new Error('File.contentType');
}
}

const file: aas.File = {
...this.writeSubmodelElementType(source),
contentType: source.contentType
contentType
};

if (source.value) {
Expand All @@ -366,7 +371,7 @@ export class JsonWriter extends AASWriter {
}

private writeBlob(source: aas.Blob): aas.Blob {
if (!source.contentType) {
if (source.value && !source.contentType) {
throw new Error('Blob.contentType');
}

Expand Down
Loading

0 comments on commit 0222718

Please sign in to comment.