Skip to content

Commit 31d22a3

Browse files
committed
feat: implement mimetype (#32)
1 parent 212731b commit 31d22a3

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

example/tests/benchmark.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ const styles = StyleSheet.create({
447447
setTimeout(async () => {
448448
const files = await showOpenFilePicker({
449449
startIn: 'desktop',
450-
types: [{ description: 'PDF files', accept: { 'text/plain': ['.txt'] } }],
450+
types: [{ description: 'PDF files', accept: { 'application/pdf': ['.pdf'] } }],
451451
})
452452

453453
if (!files[0]) {

packages/react-native-fast-io/ios/HybridFileSystem.swift

+3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ class HybridFileSystem : NSObject, UIDocumentPickerDelegate, HybridFileSystemSpe
2121
func getMetadata(path: String) throws -> Metadata {
2222
let attributes = try FileManager.default.attributesOfItem(atPath: path)
2323
let fileURL = URL(fileURLWithPath: path)
24+
25+
let mimeType = UTType(filenameExtension: fileURL.pathExtension)?.preferredMIMEType ?? ""
2426

2527
return Metadata.init(
2628
name: fileURL.lastPathComponent,
2729
path: path,
2830
root: "/",
2931
size: attributes[.size] as? Double ?? 0,
32+
type: mimeType,
3033
lastModified: (attributes[.modificationDate] as? Date)?.timeIntervalSince1970 ?? 0 * 1000
3134
)
3235
}

packages/react-native-fast-io/nitrogen/generated/ios/swift/Metadata.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public extension Metadata {
1818
/**
1919
* Create a new instance of `Metadata`.
2020
*/
21-
init(name: String, path: String, root: String, size: Double, lastModified: Double) {
22-
self.init(std.string(name), std.string(path), std.string(root), size, lastModified)
21+
init(name: String, path: String, root: String, size: Double, type: String, lastModified: Double) {
22+
self.init(std.string(name), std.string(path), std.string(root), size, std.string(type), lastModified)
2323
}
2424

2525
var name: String {
@@ -66,6 +66,17 @@ public extension Metadata {
6666
}
6767
}
6868

69+
var type: String {
70+
@inline(__always)
71+
get {
72+
return String(self.__type)
73+
}
74+
@inline(__always)
75+
set {
76+
self.__type = std.string(newValue)
77+
}
78+
}
79+
6980
var lastModified: Double {
7081
@inline(__always)
7182
get {

packages/react-native-fast-io/nitrogen/generated/shared/c++/Metadata.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ namespace margelo::nitro::fastio {
3333
std::string path SWIFT_PRIVATE;
3434
std::string root SWIFT_PRIVATE;
3535
double size SWIFT_PRIVATE;
36+
std::string type SWIFT_PRIVATE;
3637
double lastModified SWIFT_PRIVATE;
3738

3839
public:
39-
explicit Metadata(std::string name, std::string path, std::string root, double size, double lastModified): name(name), path(path), root(root), size(size), lastModified(lastModified) {}
40+
explicit Metadata(std::string name, std::string path, std::string root, double size, std::string type, double lastModified): name(name), path(path), root(root), size(size), type(type), lastModified(lastModified) {}
4041
};
4142

4243
} // namespace margelo::nitro::fastio
@@ -55,6 +56,7 @@ namespace margelo::nitro {
5556
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "path")),
5657
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "root")),
5758
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "size")),
59+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "type")),
5860
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, "lastModified"))
5961
);
6062
}
@@ -64,6 +66,7 @@ namespace margelo::nitro {
6466
obj.setProperty(runtime, "path", JSIConverter<std::string>::toJSI(runtime, arg.path));
6567
obj.setProperty(runtime, "root", JSIConverter<std::string>::toJSI(runtime, arg.root));
6668
obj.setProperty(runtime, "size", JSIConverter<double>::toJSI(runtime, arg.size));
69+
obj.setProperty(runtime, "type", JSIConverter<std::string>::toJSI(runtime, arg.type));
6770
obj.setProperty(runtime, "lastModified", JSIConverter<double>::toJSI(runtime, arg.lastModified));
6871
return obj;
6972
}
@@ -76,6 +79,7 @@ namespace margelo::nitro {
7679
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "path"))) return false;
7780
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "root"))) return false;
7881
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "size"))) return false;
82+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "type"))) return false;
7983
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, "lastModified"))) return false;
8084
return true;
8185
}

packages/react-native-fast-io/src/native/fs.nitro.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type Metadata = {
77
path: string
88
root: string
99
size: number
10+
type: string
1011
lastModified: number
1112
}
1213

packages/react-native-fast-io/src/w3c/fs.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export class File extends Blob implements globalThis.File {
2424
class NativeFile extends File {
2525
nativeStream: ReadableStream<Uint8Array>
2626

27-
constructor({ name, path, size, lastModified }: Metadata) {
27+
constructor({ name, path, type, size, lastModified }: Metadata) {
2828
const inputStream = FileSystem.createInputStream(path)
2929

3030
super([], name, {
3131
lastModified,
32+
type,
3233
})
3334

3435
this.nativeStream = toReadableStream(inputStream)

0 commit comments

Comments
 (0)