2021-04-07 19:41:54 +02:00
|
|
|
import Resource from '@joplin/lib/models/Resource';
|
|
|
|
import { ResourceEntity } from '@joplin/lib/services/database/types';
|
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { CachesDirectoryPath } from 'react-native-fs';
|
|
|
|
|
2022-10-13 23:02:06 +02:00
|
|
|
// when refactoring this name, make sure to refactor the `SharePackage.java` (in android) as well
|
2021-04-07 19:41:54 +02:00
|
|
|
const DIR_NAME = 'sharedFiles';
|
|
|
|
|
2023-10-02 20:14:08 +02:00
|
|
|
const makeShareCacheDirectory = async () => {
|
|
|
|
const targetDir = `${CachesDirectoryPath}/${DIR_NAME}`;
|
|
|
|
await shim.fsDriver().mkdir(targetDir);
|
|
|
|
|
|
|
|
return targetDir;
|
|
|
|
};
|
|
|
|
|
2023-06-30 10:55:56 +02:00
|
|
|
// Copy a file to be shared to cache, renaming it to its orignal name
|
2021-04-07 19:41:54 +02:00
|
|
|
export async function copyToCache(resource: ResourceEntity): Promise<string> {
|
|
|
|
const filename = Resource.friendlySafeFilename(resource);
|
|
|
|
|
2023-10-02 20:14:08 +02:00
|
|
|
const targetDir = await makeShareCacheDirectory();
|
2021-04-07 19:41:54 +02:00
|
|
|
const targetFile = `${targetDir}/${filename}`;
|
|
|
|
|
|
|
|
await shim.fsDriver().copy(Resource.fullPath(resource), targetFile);
|
|
|
|
|
|
|
|
return targetFile;
|
|
|
|
}
|
|
|
|
|
2023-10-02 20:14:08 +02:00
|
|
|
// fileName should be unique -- any file with fileName will be overwritten if it already exists.
|
|
|
|
export const writeTextToCacheFile = async (text: string, fileName: string): Promise<string> => {
|
|
|
|
const targetDir = await makeShareCacheDirectory();
|
|
|
|
|
|
|
|
const filePath = `${targetDir}/${fileName}`;
|
|
|
|
await shim.fsDriver().writeFile(filePath, text, 'utf8');
|
|
|
|
|
|
|
|
return filePath;
|
|
|
|
};
|
|
|
|
|
2023-06-30 10:55:56 +02:00
|
|
|
// Clear previously shared files from cache
|
2021-04-07 19:41:54 +02:00
|
|
|
export async function clearSharedFilesCache(): Promise<void> {
|
|
|
|
return shim.fsDriver().remove(`${CachesDirectoryPath}/sharedFiles`);
|
|
|
|
}
|