@simplysm/capacitor-plugin-usb-storage 12.15.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ apply plugin: 'com.android.library'
2
+
3
+ android {
4
+ namespace "kr.co.simplysm.capacitor.usbstorage"
5
+ }
6
+
7
+ dependencies {
8
+ implementation project(':capacitor-android')
9
+ implementation 'me.jahnen.libaums:core:0.9.1'
10
+ }
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ </manifest>
@@ -0,0 +1,263 @@
1
+ package kr.co.simplysm.capacitor.usbstorage;
2
+
3
+ import android.app.PendingIntent;
4
+ import android.content.BroadcastReceiver;
5
+ import android.content.Context;
6
+ import android.content.Intent;
7
+ import android.content.IntentFilter;
8
+ import android.hardware.usb.UsbDevice;
9
+ import android.hardware.usb.UsbManager;
10
+ import android.os.Build;
11
+ import android.util.Base64;
12
+ import android.util.Log;
13
+
14
+ import com.getcapacitor.JSArray;
15
+ import com.getcapacitor.JSObject;
16
+ import com.getcapacitor.Plugin;
17
+ import com.getcapacitor.PluginCall;
18
+ import com.getcapacitor.PluginMethod;
19
+ import com.getcapacitor.annotation.CapacitorPlugin;
20
+
21
+ import java.nio.ByteBuffer;
22
+ import java.util.Arrays;
23
+ import java.util.Optional;
24
+
25
+ import me.jahnen.libaums.core.UsbMassStorageDevice;
26
+ import me.jahnen.libaums.core.fs.FileSystem;
27
+ import me.jahnen.libaums.core.fs.UsbFile;
28
+ import me.jahnen.libaums.core.fs.UsbFileInputStream;
29
+
30
+ @CapacitorPlugin(name = "UsbStorage")
31
+ public class UsbStoragePlugin extends Plugin {
32
+
33
+ private static final String TAG = "UsbStoragePlugin";
34
+ private static final String ACTION_USB_PERMISSION = "kr.co.simplysm.capacitor.usbstorage.USB_PERMISSION";
35
+
36
+ @PluginMethod
37
+ public void getDevices(PluginCall call) {
38
+ try {
39
+ UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(getContext());
40
+
41
+ JSArray result = new JSArray();
42
+ for (UsbMassStorageDevice device : devices) {
43
+ UsbDevice usbDevice = device.getUsbDevice();
44
+
45
+ JSObject deviceObj = new JSObject();
46
+ deviceObj.put("deviceName", usbDevice.getDeviceName());
47
+ deviceObj.put("manufacturerName", usbDevice.getManufacturerName());
48
+ deviceObj.put("productName", usbDevice.getProductName());
49
+ deviceObj.put("vendorId", usbDevice.getVendorId());
50
+ deviceObj.put("productId", usbDevice.getProductId());
51
+ result.put(deviceObj);
52
+ }
53
+
54
+ JSObject ret = new JSObject();
55
+ ret.put("devices", result);
56
+ call.resolve(ret);
57
+ } catch (Exception e) {
58
+ Log.e(TAG, "getDevices failed", e);
59
+ call.reject("getDevices failed: " + e.getMessage());
60
+ }
61
+ }
62
+
63
+ @PluginMethod
64
+ public void requestPermission(PluginCall call) {
65
+ Integer vendorId = call.getInt("vendorId");
66
+ Integer productId = call.getInt("productId");
67
+
68
+ if (vendorId == null || productId == null) {
69
+ call.reject("vendorId and productId are required");
70
+ return;
71
+ }
72
+
73
+ try {
74
+ UsbMassStorageDevice device = getDevice(vendorId, productId);
75
+ UsbDevice usbDevice = device.getUsbDevice();
76
+
77
+ UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
78
+ if (usbManager.hasPermission(usbDevice)) {
79
+ JSObject ret = new JSObject();
80
+ ret.put("granted", true);
81
+ call.resolve(ret);
82
+ return;
83
+ }
84
+
85
+ BroadcastReceiver receiver = new BroadcastReceiver() {
86
+ @Override
87
+ public void onReceive(Context context, Intent intent) {
88
+ try {
89
+ context.unregisterReceiver(this);
90
+ boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
91
+ JSObject ret = new JSObject();
92
+ ret.put("granted", granted);
93
+ call.resolve(ret);
94
+ } catch (Exception e) {
95
+ Log.e(TAG, "requestPermission callback failed", e);
96
+ call.reject("requestPermission failed: " + e.getMessage());
97
+ }
98
+ }
99
+ };
100
+
101
+ IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
102
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
103
+ getContext().registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
104
+ } else {
105
+ getContext().registerReceiver(receiver, filter);
106
+ }
107
+
108
+ int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
109
+ ? PendingIntent.FLAG_MUTABLE
110
+ : 0;
111
+ PendingIntent permissionIntent = PendingIntent.getBroadcast(
112
+ getContext(), 0, new Intent(ACTION_USB_PERMISSION), flags
113
+ );
114
+ usbManager.requestPermission(usbDevice, permissionIntent);
115
+ } catch (Exception e) {
116
+ Log.e(TAG, "requestPermission failed", e);
117
+ call.reject("requestPermission failed: " + e.getMessage());
118
+ }
119
+ }
120
+
121
+ @PluginMethod
122
+ public void hasPermission(PluginCall call) {
123
+ Integer vendorId = call.getInt("vendorId");
124
+ Integer productId = call.getInt("productId");
125
+
126
+ if (vendorId == null || productId == null) {
127
+ call.reject("vendorId and productId are required");
128
+ return;
129
+ }
130
+
131
+ try {
132
+ UsbMassStorageDevice device = getDevice(vendorId, productId);
133
+ UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
134
+ boolean hasPermission = usbManager.hasPermission(device.getUsbDevice());
135
+
136
+ JSObject ret = new JSObject();
137
+ ret.put("granted", hasPermission);
138
+ call.resolve(ret);
139
+ } catch (Exception e) {
140
+ Log.e(TAG, "hasPermission failed", e);
141
+ call.reject("hasPermission failed: " + e.getMessage());
142
+ }
143
+ }
144
+
145
+ @PluginMethod
146
+ public void readdir(PluginCall call) {
147
+ Integer vendorId = call.getInt("vendorId");
148
+ Integer productId = call.getInt("productId");
149
+ String path = call.getString("path");
150
+
151
+ if (vendorId == null || productId == null || path == null) {
152
+ call.reject("vendorId, productId, and path are required");
153
+ return;
154
+ }
155
+
156
+ try {
157
+ UsbMassStorageDevice device = getDevice(vendorId, productId);
158
+
159
+ UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
160
+ if (!usbManager.hasPermission(device.getUsbDevice())) {
161
+ call.reject("USB 장치에 대한 접근 권한이 없습니다.");
162
+ return;
163
+ }
164
+
165
+ device.init();
166
+
167
+ FileSystem fs = device.getPartitions().get(0).getFileSystem();
168
+ UsbFile root = fs.getRootDirectory();
169
+ UsbFile dir = root.search(path);
170
+
171
+ if (dir == null || !dir.isDirectory()) {
172
+ call.reject("Directory not found: " + path);
173
+ return;
174
+ }
175
+
176
+ UsbFile[] files = dir.listFiles();
177
+
178
+ JSArray result = new JSArray();
179
+ for (UsbFile file : files) {
180
+ result.put(file.getName());
181
+ }
182
+
183
+ JSObject ret = new JSObject();
184
+ ret.put("files", result);
185
+ call.resolve(ret);
186
+ } catch (Exception e) {
187
+ Log.e(TAG, "readdir failed", e);
188
+ call.reject("readdir failed: " + e.getMessage());
189
+ }
190
+ }
191
+
192
+ @PluginMethod
193
+ public void read(PluginCall call) {
194
+ Integer vendorId = call.getInt("vendorId");
195
+ Integer productId = call.getInt("productId");
196
+ String path = call.getString("path");
197
+
198
+ if (vendorId == null || productId == null || path == null) {
199
+ call.reject("vendorId, productId, and path are required");
200
+ return;
201
+ }
202
+
203
+ try {
204
+ UsbMassStorageDevice device = getDevice(vendorId, productId);
205
+
206
+ UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
207
+ if (!usbManager.hasPermission(device.getUsbDevice())) {
208
+ call.reject("USB 장치에 대한 접근 권한이 없습니다.");
209
+ return;
210
+ }
211
+
212
+ device.init();
213
+
214
+ FileSystem fs = device.getPartitions().get(0).getFileSystem();
215
+ UsbFile root = fs.getRootDirectory();
216
+ UsbFile usbFile = root.search(path);
217
+
218
+ if (usbFile == null) {
219
+ JSObject ret = new JSObject();
220
+ ret.put("data", (String) null);
221
+ call.resolve(ret);
222
+ return;
223
+ }
224
+
225
+ if (usbFile.isDirectory()) {
226
+ call.reject("해당 경로는 폴더입니다.");
227
+ return;
228
+ }
229
+
230
+ ByteBuffer buffer = ByteBuffer.allocate((int) usbFile.getLength());
231
+
232
+ UsbFileInputStream inputStream = new UsbFileInputStream(usbFile);
233
+ byte[] tmpBuf = new byte[fs.getChunkSize()];
234
+ int count;
235
+ while ((count = inputStream.read(tmpBuf)) != -1) {
236
+ buffer.put(tmpBuf, 0, count);
237
+ }
238
+ inputStream.close();
239
+
240
+ String base64Data = Base64.encodeToString(buffer.array(), Base64.NO_WRAP);
241
+
242
+ JSObject ret = new JSObject();
243
+ ret.put("data", base64Data);
244
+ call.resolve(ret);
245
+ } catch (Exception e) {
246
+ Log.e(TAG, "read failed", e);
247
+ call.reject("read failed: " + e.getMessage());
248
+ }
249
+ }
250
+
251
+ private UsbMassStorageDevice getDevice(int vendorId, int productId) throws Exception {
252
+ UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(getContext());
253
+ Optional<UsbMassStorageDevice> optDevice = Arrays.stream(devices).filter((tmpDevice) -> {
254
+ UsbDevice tmpUsbDevice = tmpDevice.getUsbDevice();
255
+ return tmpUsbDevice.getVendorId() == vendorId && tmpUsbDevice.getProductId() == productId;
256
+ }).findFirst();
257
+
258
+ if (!optDevice.isPresent()) {
259
+ throw new Exception("USB 장치를 찾을 수 없습니다.");
260
+ }
261
+ return optDevice.get();
262
+ }
263
+ }
@@ -0,0 +1,32 @@
1
+ export interface IUsbDeviceInfo {
2
+ deviceName: string;
3
+ manufacturerName: string;
4
+ productName: string;
5
+ vendorId: number;
6
+ productId: number;
7
+ }
8
+ export interface IUsbDeviceFilter {
9
+ vendorId: number;
10
+ productId: number;
11
+ }
12
+ export interface IUsbStoragePlugin {
13
+ getDevices(): Promise<{
14
+ devices: IUsbDeviceInfo[];
15
+ }>;
16
+ requestPermission(options: IUsbDeviceFilter): Promise<{
17
+ granted: boolean;
18
+ }>;
19
+ hasPermission(options: IUsbDeviceFilter): Promise<{
20
+ granted: boolean;
21
+ }>;
22
+ readdir(options: IUsbDeviceFilter & {
23
+ path: string;
24
+ }): Promise<{
25
+ files: string[];
26
+ }>;
27
+ read(options: IUsbDeviceFilter & {
28
+ path: string;
29
+ }): Promise<{
30
+ data: string | null;
31
+ }>;
32
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,51 @@
1
+ import { IUsbDeviceInfo } from "./IUsbStoragePlugin";
2
+ /**
3
+ * USB 저장장치와 상호작용하기 위한 플러그인
4
+ * - Android: libaums 라이브러리를 통한 USB Mass Storage 접근
5
+ * - Browser: alert으로 안내 후 빈 값 반환
6
+ */
7
+ export declare abstract class UsbStorage {
8
+ /**
9
+ * 연결된 USB 장치 목록을 가져옴
10
+ * @returns 연결된 USB 장치 정보 배열
11
+ */
12
+ static getDevices(): Promise<IUsbDeviceInfo[]>;
13
+ /**
14
+ * USB 장치 접근 권한을 요청
15
+ * @param filter 권한을 요청할 USB 장치의 vendorId와 productId
16
+ * @returns 권한 승인 여부
17
+ */
18
+ static requestPermission(filter: {
19
+ vendorId: number;
20
+ productId: number;
21
+ }): Promise<boolean>;
22
+ /**
23
+ * USB 장치 접근 권한이 있는지 확인
24
+ * @param filter 권한을 확인할 USB 장치의 vendorId와 productId
25
+ * @returns 권한 보유 여부
26
+ */
27
+ static hasPermission(filter: {
28
+ vendorId: number;
29
+ productId: number;
30
+ }): Promise<boolean>;
31
+ /**
32
+ * USB 저장장치의 디렉토리 내용을 읽어옴
33
+ * @param filter 대상 USB 장치의 vendorId와 productId
34
+ * @param dirPath 읽어올 디렉토리 경로
35
+ * @returns 디렉토리 내 파일/폴더 이름 배열
36
+ */
37
+ static readdir(filter: {
38
+ vendorId: number;
39
+ productId: number;
40
+ }, dirPath: string): Promise<string[]>;
41
+ /**
42
+ * USB 저장장치의 파일을 읽어옴
43
+ * @param filter 대상 USB 장치의 vendorId와 productId
44
+ * @param filePath 읽어올 파일 경로
45
+ * @returns 파일 데이터를 담은 Buffer 또는 undefined
46
+ */
47
+ static read(filter: {
48
+ vendorId: number;
49
+ productId: number;
50
+ }, filePath: string): Promise<Buffer | undefined>;
51
+ }
@@ -0,0 +1,63 @@
1
+ import { registerPlugin } from "@capacitor/core";
2
+ const UsbStoragePlugin = registerPlugin("UsbStorage", {
3
+ web: async () => {
4
+ const { UsbStorageWeb } = await import("./web/UsbStorageWeb");
5
+ return new UsbStorageWeb();
6
+ },
7
+ });
8
+ /**
9
+ * USB 저장장치와 상호작용하기 위한 플러그인
10
+ * - Android: libaums 라이브러리를 통한 USB Mass Storage 접근
11
+ * - Browser: alert으로 안내 후 빈 값 반환
12
+ */
13
+ export class UsbStorage {
14
+ /**
15
+ * 연결된 USB 장치 목록을 가져옴
16
+ * @returns 연결된 USB 장치 정보 배열
17
+ */
18
+ static async getDevices() {
19
+ const result = await UsbStoragePlugin.getDevices();
20
+ return result.devices;
21
+ }
22
+ /**
23
+ * USB 장치 접근 권한을 요청
24
+ * @param filter 권한을 요청할 USB 장치의 vendorId와 productId
25
+ * @returns 권한 승인 여부
26
+ */
27
+ static async requestPermission(filter) {
28
+ const result = await UsbStoragePlugin.requestPermission(filter);
29
+ return result.granted;
30
+ }
31
+ /**
32
+ * USB 장치 접근 권한이 있는지 확인
33
+ * @param filter 권한을 확인할 USB 장치의 vendorId와 productId
34
+ * @returns 권한 보유 여부
35
+ */
36
+ static async hasPermission(filter) {
37
+ const result = await UsbStoragePlugin.hasPermission(filter);
38
+ return result.granted;
39
+ }
40
+ /**
41
+ * USB 저장장치의 디렉토리 내용을 읽어옴
42
+ * @param filter 대상 USB 장치의 vendorId와 productId
43
+ * @param dirPath 읽어올 디렉토리 경로
44
+ * @returns 디렉토리 내 파일/폴더 이름 배열
45
+ */
46
+ static async readdir(filter, dirPath) {
47
+ const result = await UsbStoragePlugin.readdir({ ...filter, path: dirPath });
48
+ return result.files;
49
+ }
50
+ /**
51
+ * USB 저장장치의 파일을 읽어옴
52
+ * @param filter 대상 USB 장치의 vendorId와 productId
53
+ * @param filePath 읽어올 파일 경로
54
+ * @returns 파일 데이터를 담은 Buffer 또는 undefined
55
+ */
56
+ static async read(filter, filePath) {
57
+ const result = await UsbStoragePlugin.read({ ...filter, path: filePath });
58
+ if (result.data == null) {
59
+ return undefined;
60
+ }
61
+ return Buffer.from(result.data, "base64");
62
+ }
63
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./IUsbStoragePlugin";
2
+ export * from "./UsbStorage";
3
+ export * from "./web/UsbStorageWeb";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./IUsbStoragePlugin";
2
+ export * from "./UsbStorage";
3
+ export * from "./web/UsbStorageWeb";
@@ -0,0 +1,23 @@
1
+ import { WebPlugin } from "@capacitor/core";
2
+ import { IUsbDeviceFilter, IUsbDeviceInfo, IUsbStoragePlugin } from "../IUsbStoragePlugin";
3
+ export declare class UsbStorageWeb extends WebPlugin implements IUsbStoragePlugin {
4
+ getDevices(): Promise<{
5
+ devices: IUsbDeviceInfo[];
6
+ }>;
7
+ requestPermission(_options: IUsbDeviceFilter): Promise<{
8
+ granted: boolean;
9
+ }>;
10
+ hasPermission(_options: IUsbDeviceFilter): Promise<{
11
+ granted: boolean;
12
+ }>;
13
+ readdir(_options: IUsbDeviceFilter & {
14
+ path: string;
15
+ }): Promise<{
16
+ files: string[];
17
+ }>;
18
+ read(_options: IUsbDeviceFilter & {
19
+ path: string;
20
+ }): Promise<{
21
+ data: string | null;
22
+ }>;
23
+ }
@@ -0,0 +1,21 @@
1
+ import { WebPlugin } from "@capacitor/core";
2
+ export class UsbStorageWeb extends WebPlugin {
3
+ async getDevices() {
4
+ return await Promise.resolve({ devices: [] });
5
+ }
6
+ async requestPermission(_options) {
7
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
8
+ return await Promise.resolve({ granted: false });
9
+ }
10
+ async hasPermission(_options) {
11
+ return await Promise.resolve({ granted: false });
12
+ }
13
+ async readdir(_options) {
14
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
15
+ return await Promise.resolve({ files: [] });
16
+ }
17
+ async read(_options) {
18
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
19
+ return await Promise.resolve({ data: null });
20
+ }
21
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@simplysm/capacitor-plugin-usb-storage",
3
+ "version": "12.15.69",
4
+ "description": "심플리즘 패키지 - Capacitor USB Storage Plugin",
5
+ "author": "김석래",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/kslhunter/simplysm.git",
9
+ "directory": "packages/capacitor-plugin-usb-storage"
10
+ },
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "capacitor": {
16
+ "android": {
17
+ "src": "android"
18
+ }
19
+ },
20
+ "dependencies": {
21
+ "@capacitor/core": "^7.4.4"
22
+ }
23
+ }
@@ -0,0 +1,20 @@
1
+ export interface IUsbDeviceInfo {
2
+ deviceName: string;
3
+ manufacturerName: string;
4
+ productName: string;
5
+ vendorId: number;
6
+ productId: number;
7
+ }
8
+
9
+ export interface IUsbDeviceFilter {
10
+ vendorId: number;
11
+ productId: number;
12
+ }
13
+
14
+ export interface IUsbStoragePlugin {
15
+ getDevices(): Promise<{ devices: IUsbDeviceInfo[] }>;
16
+ requestPermission(options: IUsbDeviceFilter): Promise<{ granted: boolean }>;
17
+ hasPermission(options: IUsbDeviceFilter): Promise<{ granted: boolean }>;
18
+ readdir(options: IUsbDeviceFilter & { path: string }): Promise<{ files: string[] }>;
19
+ read(options: IUsbDeviceFilter & { path: string }): Promise<{ data: string | null }>;
20
+ }
@@ -0,0 +1,82 @@
1
+ import { registerPlugin } from "@capacitor/core";
2
+ import { IUsbDeviceInfo, IUsbStoragePlugin } from "./IUsbStoragePlugin";
3
+
4
+ const UsbStoragePlugin = registerPlugin<IUsbStoragePlugin>("UsbStorage", {
5
+ web: async () => {
6
+ const { UsbStorageWeb } = await import("./web/UsbStorageWeb");
7
+ return new UsbStorageWeb();
8
+ },
9
+ });
10
+
11
+ /**
12
+ * USB 저장장치와 상호작용하기 위한 플러그인
13
+ * - Android: libaums 라이브러리를 통한 USB Mass Storage 접근
14
+ * - Browser: alert으로 안내 후 빈 값 반환
15
+ */
16
+ export abstract class UsbStorage {
17
+ /**
18
+ * 연결된 USB 장치 목록을 가져옴
19
+ * @returns 연결된 USB 장치 정보 배열
20
+ */
21
+ static async getDevices(): Promise<IUsbDeviceInfo[]> {
22
+ const result = await UsbStoragePlugin.getDevices();
23
+ return result.devices;
24
+ }
25
+
26
+ /**
27
+ * USB 장치 접근 권한을 요청
28
+ * @param filter 권한을 요청할 USB 장치의 vendorId와 productId
29
+ * @returns 권한 승인 여부
30
+ */
31
+ static async requestPermission(filter: {
32
+ vendorId: number;
33
+ productId: number;
34
+ }): Promise<boolean> {
35
+ const result = await UsbStoragePlugin.requestPermission(filter);
36
+ return result.granted;
37
+ }
38
+
39
+ /**
40
+ * USB 장치 접근 권한이 있는지 확인
41
+ * @param filter 권한을 확인할 USB 장치의 vendorId와 productId
42
+ * @returns 권한 보유 여부
43
+ */
44
+ static async hasPermission(filter: {
45
+ vendorId: number;
46
+ productId: number;
47
+ }): Promise<boolean> {
48
+ const result = await UsbStoragePlugin.hasPermission(filter);
49
+ return result.granted;
50
+ }
51
+
52
+ /**
53
+ * USB 저장장치의 디렉토리 내용을 읽어옴
54
+ * @param filter 대상 USB 장치의 vendorId와 productId
55
+ * @param dirPath 읽어올 디렉토리 경로
56
+ * @returns 디렉토리 내 파일/폴더 이름 배열
57
+ */
58
+ static async readdir(
59
+ filter: { vendorId: number; productId: number },
60
+ dirPath: string,
61
+ ): Promise<string[]> {
62
+ const result = await UsbStoragePlugin.readdir({ ...filter, path: dirPath });
63
+ return result.files;
64
+ }
65
+
66
+ /**
67
+ * USB 저장장치의 파일을 읽어옴
68
+ * @param filter 대상 USB 장치의 vendorId와 productId
69
+ * @param filePath 읽어올 파일 경로
70
+ * @returns 파일 데이터를 담은 Buffer 또는 undefined
71
+ */
72
+ static async read(
73
+ filter: { vendorId: number; productId: number },
74
+ filePath: string,
75
+ ): Promise<Buffer | undefined> {
76
+ const result = await UsbStoragePlugin.read({ ...filter, path: filePath });
77
+ if (result.data == null) {
78
+ return undefined;
79
+ }
80
+ return Buffer.from(result.data, "base64");
81
+ }
82
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./IUsbStoragePlugin";
2
+ export * from "./UsbStorage";
3
+ export * from "./web/UsbStorageWeb";
@@ -0,0 +1,27 @@
1
+ import { WebPlugin } from "@capacitor/core";
2
+ import { IUsbDeviceFilter, IUsbDeviceInfo, IUsbStoragePlugin } from "../IUsbStoragePlugin";
3
+
4
+ export class UsbStorageWeb extends WebPlugin implements IUsbStoragePlugin {
5
+ async getDevices(): Promise<{ devices: IUsbDeviceInfo[] }> {
6
+ return await Promise.resolve({ devices: [] });
7
+ }
8
+
9
+ async requestPermission(_options: IUsbDeviceFilter): Promise<{ granted: boolean }> {
10
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
11
+ return await Promise.resolve({ granted: false });
12
+ }
13
+
14
+ async hasPermission(_options: IUsbDeviceFilter): Promise<{ granted: boolean }> {
15
+ return await Promise.resolve({ granted: false });
16
+ }
17
+
18
+ async readdir(_options: IUsbDeviceFilter & { path: string }): Promise<{ files: string[] }> {
19
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
20
+ return await Promise.resolve({ files: [] });
21
+ }
22
+
23
+ async read(_options: IUsbDeviceFilter & { path: string }): Promise<{ data: string | null }> {
24
+ alert("[UsbStorage] 웹 환경에서는 USB 저장장치 접근을 지원하지 않습니다.");
25
+ return await Promise.resolve({ data: null });
26
+ }
27
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2021", "DOM"],
5
+ "outDir": "./dist"
6
+ },
7
+ "exclude": ["dist", "node_modules"]
8
+ }