Skip to content

Commit 91892b9

Browse files
Merge pull request #468 from appwrite/feat-improve-deno-file-upload
Feat: More options for deno file upload
2 parents 71205eb + ef502e1 commit 91892b9

File tree

9 files changed

+234
-117
lines changed

9 files changed

+234
-117
lines changed

composer.lock

Lines changed: 76 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SDK/Language/Deno.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function getFiles()
3636
'template' => 'deno/src/query.ts.twig',
3737
'minify' => false,
3838
],
39+
[
40+
'scope' => 'default',
41+
'destination' => 'src/inputFile.ts',
42+
'template' => 'deno/src/inputFile.ts.twig',
43+
'minify' => false,
44+
],
3945
[
4046
'scope' => 'default',
4147
'destination' => '/src/service.ts',
@@ -101,7 +107,7 @@ public function getTypeName($type)
101107
return 'string';
102108
break;
103109
case self::TYPE_FILE:
104-
return 'string';
110+
return 'InputFile';
105111
break;
106112
case self::TYPE_BOOLEAN:
107113
return 'boolean';

templates/deno/mod.ts.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Client } from "./src/client.ts";
22
import { Query } from "./src/query.ts";
3+
import { InputFile } from "./src/inputFile.ts";
34
import { {{spec.title | caseUcfirst}}Exception } from "./src/exception.ts";
45
{% for service in spec.services %}
56
import { {{service.name | caseUcfirst}} } from "./src/services/{{service.name | caseDash}}.ts";
@@ -8,6 +9,7 @@ import { {{service.name | caseUcfirst}} } from "./src/services/{{service.name |
89
export {
910
Client,
1011
Query,
12+
InputFile,
1113
{{spec.title | caseUcfirst}}Exception,
1214
{% for service in spec.services %}
1315
{{service.name | caseUcfirst}},

templates/deno/src/client.ts.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ export class Client {
7878
const formData = new FormData();
7979
const flatParams = this.flatten(params);
8080
for (const key in flatParams) {
81-
formData.append(key, flatParams[key]);
81+
const value = flatParams[key];
82+
83+
if(value && value.type && value.type === 'file') {
84+
formData.append(key, value.file, value.filename);
85+
} else {
86+
formData.append(key, flatParams[key]);
87+
}
8288
}
8389
body = formData;
8490
} else {

templates/deno/src/inputFile.ts.twig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { readableStreamFromReader } from "https://deno.land/std/streams/conversion.ts";
2+
3+
const _bufferToString = (buffer: Uint8Array): ReadableStream<Uint8Array> => {
4+
return new ReadableStream({
5+
start(controller) {
6+
controller.enqueue(buffer);
7+
controller.close();
8+
}
9+
});
10+
};
11+
12+
export class InputFile {
13+
stream: ReadableStream<Uint8Array>; // Content of file as a stream
14+
size: number; // Total final size of the file content
15+
filename: string; // File name
16+
17+
static fromPath = (filePath: string, filename: string): InputFile => {
18+
const file = Deno.openSync(filePath);
19+
const stream = readableStreamFromReader(file);
20+
const size = Deno.statSync(filePath).size;
21+
return new InputFile(stream, filename, size);
22+
};
23+
24+
static fromBuffer = (buffer: Uint8Array, filename: string): InputFile => {
25+
const stream = _bufferToString(buffer);
26+
const size = buffer.byteLength;
27+
return new InputFile(stream, filename, size);
28+
};
29+
30+
static fromPlainText = (content: string, filename: string): InputFile => {
31+
const buffer = new TextEncoder().encode(content);
32+
const stream = _bufferToString(buffer);
33+
const size = buffer.byteLength;
34+
return new InputFile(stream, filename, size);
35+
};
36+
37+
constructor(stream: ReadableStream<Uint8Array>, filename: string, size: number) {
38+
this.stream = stream;
39+
this.filename = filename;
40+
this.size = size;
41+
}
42+
}

0 commit comments

Comments
 (0)