Skip to content

Commit b49d668

Browse files
committed
added support for file content in Readable, Blob and ArrayBuffer
1 parent 356cb51 commit b49d668

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ await app.initializeAsync();
3838
### Training
3939
```typescript
4040
await app.train.addPersonAsync(person: PersonModel): Promise<PersonModel>
41-
await app.train.addPersonFaceAsync(personId: string, photo: Blob, rectangle: RectangleModel): Promise<void>
41+
await app.train.addPersonFaceAsync(personId: string, photo: Readable | Blob | ArrayBuffer, rectangle: RectangleModel): Promise<void>
4242
```
4343

4444
### Predicting
4545
```typescript
46-
await app.predict.detectFacesAsync(photo: Blob): Promise<DetectFaceModel[]>
47-
await app.predict.identifyFacesAsync(photo: Blob): Promise<IdentifyModel[]>
46+
await app.predict.detectFacesAsync(photo: Readable | Blob | ArrayBuffer): Promise<DetectFaceModel[]>
47+
await app.predict.identifyFacesAsync(photo: Readable | Blob | ArrayBuffer): Promise<IdentifyModel[]>
4848
```
4949

5050
## Known issues

Diff for: src/application/PredictApplication.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ import FaceService from "../service/FaceService";
66
import {IdentifyModel} from "./model/Identify/IdentifyModel";
77
import ConfigService from "../service/config/ConfigService";
88

9+
import {Readable} from "stream";
10+
911
export default class PredictApplication extends ApplicationBase {
1012
constructor(log: ILogService) {
1113
super(log);
1214
}
1315

14-
public async detectFacesAsync(photo: Blob): Promise<DetectFaceModel[]> {
16+
public async detectFacesAsync(photo: Readable | Blob | ArrayBuffer): Promise<DetectFaceModel[]> {
1517
this.log.info("Uploading photo.");
1618
const url = await BlobService.uploadBlobAsync(photo);
1719

1820
this.log.info("Detecting faces.");
1921
return await FaceService.detectFacesAsync(url);
2022
}
2123

22-
public async identifyFacesAsync(photo: Blob): Promise<IdentifyModel[]> {
24+
public async identifyFacesAsync(photo: Readable | Blob | ArrayBuffer): Promise<IdentifyModel[]> {
2325
this.log.info("Uploading photo.");
2426
const url = await BlobService.uploadBlobAsync(photo);
2527

Diff for: src/application/TrainApplication.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {PersonModel} from "./model/Person/PersonModel";
1212
import BlobService from "../service/BlobService";
1313
import {RectangleModel} from "./model/Face/Base/RectangleModel";
1414

15+
import {Readable} from "stream";
16+
1517
export default class TrainApplication extends ApplicationBase {
1618
constructor(log: ILogService) {
1719
super(log);
@@ -22,7 +24,7 @@ export default class TrainApplication extends ApplicationBase {
2224
return await FaceService.createPersonAsync(ConfigService.Face.Group.Id, person);
2325
}
2426

25-
public async addPersonFaceAsync(personId: string, photo: Blob, rectangle: RectangleModel): Promise<void> {
27+
public async addPersonFaceAsync(personId: string, photo: Readable | Blob | ArrayBuffer, rectangle: RectangleModel): Promise<void> {
2628
this.log.info("Uploading photo.");
2729
const url = await BlobService.uploadBlobAsync(photo);
2830

Diff for: src/service/BlobService.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ConfigService from "./config/ConfigService";
22
import GeneratorService from "./helper/GeneratorService";
33

44
import {BlobServiceClient, ContainerClient, StorageSharedKeyCredential} from "@azure/storage-blob";
5+
import {Readable} from "stream";
56

67
export default class BlobService {
78
public static async createContainerIfNotExistsAsync(): Promise<void> {
@@ -23,7 +24,7 @@ export default class BlobService {
2324
/**
2425
* @param content Blob.
2526
*/
26-
public static async uploadBlobAsync(content: Blob): Promise<string> {
27+
public static async uploadBlobAsync(content: Readable | Blob | ArrayBuffer): Promise<string> {
2728
// TODO: refactoring needed
2829
const size = 10;
2930
let uniqueId = GeneratorService.getUniqieNumber(size).toString();
@@ -34,7 +35,15 @@ export default class BlobService {
3435
const clientContainer = this.getClientContainer();
3536
const clientBlob = clientContainer.getBlockBlobClient(uniqueId);
3637

37-
await clientBlob.upload(content, content.size);
38+
if (content instanceof ArrayBuffer) {
39+
await clientBlob.upload(content, content.byteLength);
40+
} else if (content instanceof Readable) {
41+
await clientBlob.uploadStream(content);
42+
} else if (content instanceof Blob) {
43+
await clientBlob.upload(content, content.size);
44+
} else {
45+
throw new Error("Content should be either Readable or Blob");
46+
}
3847

3948
return clientBlob.url;
4049
}

Diff for: test/integration/service/BlobService.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import SecretService from "../../../src/service/config/SecretService";
2+
import ConfigService from "../../../src/service/config/ConfigService";
3+
import BlobService from "../../../src/service/BlobService";
4+
5+
import * as fs from "fs";
6+
import * as path from "path";
7+
8+
before(async () => {
9+
SecretService.Blob.Name = "";
10+
SecretService.Blob.Key = "";
11+
ConfigService.initializeBlob();
12+
await BlobService.createContainerIfNotExistsAsync();
13+
});
14+
15+
describe("DIY IoT lock Application - blob service", () => {
16+
const filePath = path.join(__dirname, "test.jpg");
17+
18+
it("upload file by content", async () => {
19+
const content = fs.readFileSync(filePath);
20+
const blob = new Uint8Array(content).buffer;
21+
await BlobService.uploadBlobAsync(blob);
22+
//TODO: check manually
23+
});
24+
25+
it("upload file by stream", async () => {
26+
const stream = fs.createReadStream(filePath);
27+
await BlobService.uploadBlobAsync(stream);
28+
//TODO: check manually
29+
});
30+
});

Diff for: test/integration/service/test.jpg

112 KB
Loading

0 commit comments

Comments
 (0)