-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.tsx
143 lines (131 loc) · 4.05 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { NativeModules, PermissionsAndroid, Platform } from 'react-native';
const LINKING_ERROR =
`The package 'react-native-video-trim' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';
const VideoTrim = NativeModules.VideoTrim
? NativeModules.VideoTrim
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
export interface EditorConfig {
saveToPhoto?: boolean;
removeAfterSavedToPhoto?: boolean;
maxDuration?: number;
minDuration?: number;
cancelButtonText?: string;
saveButtonText?: string;
enableCancelDialog?: boolean;
cancelDialogTitle?: string;
cancelDialogMessage?: string;
cancelDialogCancelText?: string;
cancelDialogConfirmText?: string;
enableSaveDialog?: boolean;
saveDialogTitle?: string;
saveDialogMessage?: string;
saveDialogCancelText?: string;
saveDialogConfirmText?: string;
trimmingText?: string;
fullScreenModalIOS?: boolean;
}
/**
* Delete a file
*
* @param {string} videoPath: absolute non-empty file path to edit
* @param {EditorConfig} config: editor configuration
* @returns {void} A **Promise** which resolves `void`
*/
export async function showEditor(
filePath: string,
config: EditorConfig = {}
): Promise<void> {
if (!filePath?.trim().length) {
throw new Error('File path cannot be empty!');
}
const { saveToPhoto = true } = config;
const outputPath = await VideoTrim.showEditor(filePath, config);
if (Platform.OS === 'android') {
if (saveToPhoto) {
try {
if (Platform.Version >= 33) {
// since android 13 it's not needed to request permission for write storage: https://github.com/facebook/react-native/issues/36714#issuecomment-1491338276
await VideoTrim.saveVideo(outputPath);
if (config.removeAfterSavedToPhoto) {
deleteFile(outputPath);
}
} else {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE!,
{
title: 'Video Trimmer Photos Access Required',
message: 'Grant access to your Photos to write output Video',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
await VideoTrim.saveVideo(outputPath);
if (config.removeAfterSavedToPhoto) {
deleteFile(outputPath);
}
} else {
throw new Error('Photos Library permission denied');
}
}
} catch (err) {
throw err;
} finally {
VideoTrim.hideDialog();
}
} else {
VideoTrim.hideDialog();
}
}
}
/**
* Delete a file
*
* @param {string} filePath: absolute non-empty file path to check if editable
* @returns {Promise} A **Promise** which resolves `true` if editable
*/
export function isValidVideo(filePath: string): Promise<boolean> {
if (!filePath?.trim().length) {
throw new Error('File path cannot be empty!');
}
return VideoTrim.isValidVideo(filePath);
}
/**
* Clean output files generated at all time
*
* @returns {Promise<string[]>} A **Promise** which resolves to array of files
*/
export function listFiles(): Promise<string[]> {
return VideoTrim.listFiles();
}
/**
* Clean output files generated at all time
*
* @returns {Promise} A **Promise** which resolves to number of deleted files
*/
export function cleanFiles(): Promise<number> {
return VideoTrim.cleanFiles();
}
/**
* Delete a file
*
* @param {string} filePath: absolute non-empty file path to delete
* @returns {Promise} A **Promise** which resolves `true` if successful
*/
export function deleteFile(filePath: string): Promise<boolean> {
if (!filePath?.trim().length) {
throw new Error('File path cannot be empty!');
}
return VideoTrim.deleteFile(filePath);
}