Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use native apis to read file contents #1348

Merged
merged 9 commits into from
Oct 8, 2024
42 changes: 26 additions & 16 deletions src/routes/docs/products/functions/develop/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,9 @@ export default async ({ req, res, log }) => {
case 'json':
return res.json({"type": "This is a JSON response"});
case 'binary':
return res.binary(InputFile.fromPath('/path/to/file', 'filename'));
const bytes = fs.readFileSync('/path/to/file');
// or const bytes = await fs.readFile('/path/to/file');
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
return res.binary(bytes);
case 'redirect':
return res.redirect("https://appwrite.io", 301);
case 'html':
Expand All @@ -838,7 +840,6 @@ export default async ({ req, res, log }) => {
```
```php
<?php
use Appwrite\InputFile;

return function ($context) {
switch ($context->req->query['type']) {
Expand All @@ -847,7 +848,8 @@ return function ($context) {
case 'json':
return $context->res->json(["type" => "This is a JSON response"]);
case 'binary':
return $context->res->binary(InputFile::withPath('file.png'));
$fileContent = file_get_contents('file.png');
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
return $context->res->binary($fileContent);
case 'redirect':
return $context->res->redirect("https://appwrite.io", 301);
case 'html':
Expand All @@ -860,7 +862,6 @@ return function ($context) {
};
```
```python
from appwrite.input_file import InputFile

def main(context):
type = context.req.query['type']
Expand All @@ -870,7 +871,9 @@ def main(context):
elif type == 'json':
return context.res.json({"type": "This is a JSON response"})
elif type == 'binary':
return context.res.binary(InputFile.from_path('file.png'))
with open('file.png', 'rb') as file:
file_contents = file.read()
return context.res.binary(file_contents)
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
elif type == 'redirect':
return context.res.redirect("https://appwrite.io", 301)
elif type == 'html':
Expand All @@ -888,7 +891,8 @@ def main(context)
when 'json'
return context.res.json({"type": "This is a JSON response"})
when 'binary'
return context.res.binary(InputFile.from_path('dir/file.png'))
file_contents = File.binread('dir/file.png')
return context.res.binary(file_contents)
when 'redirect'
return context.res.redirect("https://appwrite.io", 301)
when 'html'
Expand All @@ -909,7 +913,9 @@ export default async ({ req, res, log }) => {
case 'json':
return res.json({type: "This is a JSON response"});
case 'binary':
return res.binary(InputFile.fromPath('/path/to/file.png', 'file.png'));
const fileContents = Deno.readFileSync('/path/to/file.png');
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
// or const fileContents = await Deno.readFile('/path/to/file.png');
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
return res.binary(fileContents);
case 'redirect':
return res.redirect("https://appwrite.io", 301);
case 'html':
Expand Down Expand Up @@ -939,7 +945,7 @@ func Main(Context openruntimes.Context) openruntimes.Response {
case "json":
return Context.Res.Json(map[string]string{"type": "This is a JSON response"})
case "binary":
file, _ := os.Open("./destiny.png")
file, _ := os.Open("./file.png")
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
imageData, _ := io.ReadAll(file)
return Context.Res.Binary(imageData)
case "redirect":
Expand All @@ -952,6 +958,7 @@ func Main(Context openruntimes.Context) openruntimes.Response {
}
```
```dart
import 'dart:io';
import 'dart:async';

Future<dynamic> main(final context) async {
Expand All @@ -961,7 +968,10 @@ Future<dynamic> main(final context) async {
case 'json':
return context.res.json({'type': 'This is a JSON response'});
case 'binary':
return context.res.binary(InputFile(path: './path-to-files/image.jpg', filename: 'image.jpg'));
final file = File('./path-to-files/file.jpg');
final fileContents = await file.readAsBytesSync();
// or final fileContents = await file.readAsBytes();
return context.res.binary(fileContents);
case 'redirect':
return context.res.redirect('https://appwrite.io', 301);
case 'html':
Expand All @@ -982,7 +992,8 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput {
case "json":
return context.res.text(["type": "This is a JSON response"])
case "binary":
return context.res.binary(InputFile.fromPath("file.png"))
let fileContents = FileManager.default.contents(atPath: "file.png")
return context.res.binary(fileContents)
case "redirect":
return context.res.redirect("https://appwrite.io", 301)
case "html":
Expand All @@ -1005,7 +1016,7 @@ public class Handler {
case "json":
return Context.Res.Text(new Dictionary<string, object>() { { "type", "This is a JSON response" } });
case "binary":
return Context.Res.Binary(InputFile.FromPath("./path-to-files/image.jpg"));
return Context.Res.Binary(File.ReadAllBytes("./path-to-files/file.jpg"));
case "redirect":
return Context.Res.Redirect("https://appwrite.io", 301);
case "html":
Expand All @@ -1023,14 +1034,13 @@ package io.openruntimes.kotlin.src

import io.openruntimes.kotlin.RuntimeContext
import io.openruntimes.kotlin.RuntimeOutput
import io.appwrite.models.InputFile

class Main {
fun main(context: RuntimeContext): RuntimeOutput {
when (context.req.query["type"]) {
"empty" -> return context.res.empty()
"json" -> return context.res.text(mapOf("type" to "This is a JSON response"))
"binary" -> return context.res.binary(InputFile.fromPath("file.png"))
"binary" -> return context.res.binary(File("file.png").readBytes())
"redirect" -> return context.res.redirect("https://appwrite.io", 301)
"html" -> return context.res.text("<h1>This is an HTML response</h1>", 200, mapOf("content-type" to "text/html"))
else -> return context.res.text("This is a text response")
Expand All @@ -1043,7 +1053,6 @@ package io.openruntimes.java.src;

import io.openruntimes.java.RuntimeContext;
import io.openruntimes.java.RuntimeOutput;
import io.appwrite.models.InputFile;
import java.util.Map;
import java.util.HashMap;

Expand All @@ -1057,7 +1066,7 @@ public class Main {
data.put("type", "This is a JSON response");
return context.getRes().text(data);
case "binary"
return context.getRes().binary(InputFile.fromPath("file.png"));
return context.getRes().binary(Files.readAllBytes(Paths.get("file.png")));
case "redirect":
return context.getRes().redirect("https://appwrite.io", 301);
case "html":
Expand Down Expand Up @@ -1087,7 +1096,8 @@ namespace runtime {
data["type"] = "This is a JSON response";
return context.res.text(data);
} else if (type == "binary") {
return context.res.binary(InputFile.fromPath("file.png"))
std::vector<char> buffer(std::istreambuf_iterator<char>(std::ifstream("file.png", std::ios::binary)), {});
return context.res.binary(buffer)
} else if (type == "redirect") {
return context.res.redirect("https://appwrite.io", 301);
} else if (type == "html") {
Expand Down
Loading