Skip to content

Commit

Permalink
fix : ERROR $ open failed: EPERM (Operation not permitted) (#1371)
Browse files Browse the repository at this point in the history
When you try to create a backup file on Android 11+ device you get this error 
/storage/emulated/0/h/inreader_backup_YYYY-MM-DD_HH_mm.json: open failed: EPERM (Operation not permitted)
  • Loading branch information
MOA-YAD authored Jan 4, 2025
1 parent 9636aab commit c2c8935
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/services/backup/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,40 @@ import { NovelInfo } from '@database/types';
import { sleep } from '@utils/sleep';
import { getString } from '@strings/translations';
import * as FileSystem from 'expo-file-system';
import FileManager from '@native/FileManager';

export const createBackup = async () => {
try {
const novels = await getLibraryNovelsFromDb();

const folder = await FileManager.pickFolder();
if (!folder) {
return;
}
const datetime = dayjs().format('YYYY-MM-DD_HH_mm');
const fileName = 'lnreader_backup_' + datetime + '.json';
await FileManager.writeFile(
folder + '/' + fileName,
JSON.stringify(novels),
);
showToast(getString('backupScreen.legacy.backupCreated', { fileName }));
const fileContent = JSON.stringify(novels);
const permissions =
await FileSystem.StorageAccessFramework.requestDirectoryPermissionsAsync();

if (!permissions.granted) {
showToast(getString('backupScreen.legacy.permissionDenied'));
return;
}
const directoryUri = permissions.directoryUri;
await FileSystem.StorageAccessFramework.createFileAsync(
directoryUri,
fileName,
'application/json',
)
.then(async uri => {
await FileSystem.writeAsStringAsync(uri, fileContent, {
encoding: FileSystem.EncodingType.UTF8,
});
showToast(getString('backupScreen.legacy.backupCreated', { fileName }));
})
.catch(error => {
showToast(
getString('backupScreen.legacy.failedToSave', {
message: error.message,
}),
);
});
} catch (error: any) {
showToast(error.message);
} finally {
Expand Down

0 comments on commit c2c8935

Please sign in to comment.