Skip to content

Commit

Permalink
feat: implement requestThingDescription for FileClient
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Nov 23, 2023
1 parent cd96956 commit 3a7c741
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions packages/binding-file/src/file-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,48 @@ import path = require("path");

const { debug, warn } = createLoggers("binding-file", "file-client");

/**
* Used to determine the Content-Type of a file from the extension in its
* {@link filePath} if no explicit Content-Type is defined.
*
* @param filepath The file paththe Content-Type is determined for.
* @returns An appropriate Content-Type or `application/octet-stream` as a fallback.
*/
function mapFileExtensionToContentType(filepath: string) {
const fileExtension = path.extname(filepath);
debug(`FileClient found '${fileExtension}' extension`);
switch (fileExtension) {
case ".txt":
case ".log":
case ".ini":
case ".cfg":
return "text/plain";
case ".json":
return "application/json";
case ".jsontd":
return "application/td+json";
case ".jsonld":
return "application/ld+json";
default:
warn(`FileClient cannot determine media type for path '${filepath}'`);
return "application/octet-stream";
}
}

Check warning on line 53 in packages/binding-file/src/file-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-file/src/file-client.ts#L34-L53

Added lines #L34 - L53 were not covered by tests

export default class FileClient implements ProtocolClient {
public toString(): string {
return "[FileClient]";
}

private async readFile(filepath: string, contentType?: string): Promise<Content> {
const resource = fs.createReadStream(filepath);
const resourceContentType = contentType ?? mapFileExtensionToContentType(filepath);
return new Content(resourceContentType, resource);
}

Check warning on line 65 in packages/binding-file/src/file-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-file/src/file-client.ts#L60-L65

Added lines #L60 - L65 were not covered by tests
public async readResource(form: Form): Promise<Content> {
const filepath = form.href.split("//");
const resource = fs.createReadStream(filepath[1]);
const extension = path.extname(filepath[1]);
debug(`FileClient found '${extension}' extension`);
let contentType;
if (form.contentType != null) {
contentType = form.contentType;
} else {
// *guess* contentType based on file extension
contentType = "application/octet-stream";
switch (extension) {
case ".txt":
case ".log":
case ".ini":
case ".cfg":
contentType = "text/plain";
break;
case ".json":
contentType = "application/json";
break;
case ".jsonld":
contentType = "application/ld+json";
break;
default:
warn(`FileClient cannot determine media type of '${form.href}'`);
}
}
return new Content(contentType, resource);
const filepath = form.href.split("//")[1];
return this.readFile(filepath, form.contentType);

Check warning on line 68 in packages/binding-file/src/file-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-file/src/file-client.ts#L67-L68

Added lines #L67 - L68 were not covered by tests
}

public async writeResource(form: Form, content: Content): Promise<void> {
Expand All @@ -76,7 +84,7 @@ export default class FileClient implements ProtocolClient {
* @inheritdoc
*/
public async requestThingDescription(uri: string): Promise<Content> {
throw new Error("Not implemented");
return this.readFile(uri, "application/td+json");
}

Check warning on line 89 in packages/binding-file/src/file-client.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-file/src/file-client.ts#L83-L89

Added lines #L83 - L89 were not covered by tests
public async subscribeResource(
Expand Down

0 comments on commit 3a7c741

Please sign in to comment.