Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should passes when used properly', () => {
copyAsync({ from: '', to: '' }).then(fileInfo => {
(fileInfo: void);
// $ExpectError: check any
(fileInfo: number);
});
});
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
copyAsync();
// $ExpectError: first argument must be an object
copyAsync(69);
// $ExpectError: need 'to' prop
copyAsync({ from: '' });
// $ExpectError: need 'from' prop
copyAsync({ to: '' });
copyAsync({
// $ExpectError: need string
from: 69,
// $ExpectError: need string
to: 69,
});
});
});
async function fileInfoAsync(url: ?string, name: string): Promise {
if (!url) {
throw new Error('expo-asset-utils: fileInfoAsync(): cannot load from empty url!');
return null;
}
name = name || filenameFromUri(url);
if (Platform.OS === 'web') {
return { uri: url, name, hash: null };
}
const localUri = FileSystem.cacheDirectory + name;
if (isAssetLibraryUri(url)) {
/// ios asset: we need to copy this over and then get the hash
await FileSystem.copyAsync({
from: url,
to: localUri,
});
const hash = await getHashAsync(localUri);
return { uri: localUri, name, hash };
} else if (isLocalUri(url)) {
/// local image: we just need the hash
let file = await resolveLocalFileAsync({ uri: url, name });
if (!file) {
file = await resolveLocalFileAsync({ uri: localUri, name });
if (!file) {
throw new Error(
`expo-asset-utils: fileInfoAsync(): couldn't resolve md5 hash for local uri: ${url} or alternate: ${localUri}`
);
return null;
}
async function treeSearch(relativePath, legacyPath, newPath, resolveConflict) {
const currentNewPath = `${newPath}${relativePath}`;
const currentLegacyPath = `${legacyPath}${relativePath}`;
const legacyPathInfo = await FileSystem.getInfoAsync(currentLegacyPath);
const newPathInfo = await FileSystem.getInfoAsync(currentNewPath);
if (legacyPathInfo.exists && !newPathInfo.exists) {
await FileSystem.copyAsync({
from: currentLegacyPath,
to: currentNewPath,
});
await FileSystem.deleteAsync(currentLegacyPath);
return;
}
if (legacyPathInfo.isDirectory) {
const children = await FileSystem.readDirectoryAsync(currentLegacyPath);
for (let child of children) {
await treeSearch(relativePath + `${child}/`, legacyPath, newPath, resolveConflict);
}
}
else {
await resolveConflict(currentLegacyPath, currentNewPath);
}
}
async function moveToPhotoCacheAsync(uri: string): Promise {
await ensureDirectory(directory);
const extension = getExtension(uri);
const cachedFileName = `${Date.now()}.${extension}`;
const cachedFileLocation = `${directory}/${cachedFileName}`;
await FileSystem.copyAsync({
from: uri,
to: cachedFileLocation,
});
return cachedFileLocation;
}
async function copyAssetToSameDirectoryWithNewNameAsync(
fileReference,
name: string
): Promise {
const url = await uriAsync(fileReference);
const nextUrl = replaceNameInUri(url, name);
await copyAsync({ from: url, to: nextUrl });
return nextUrl;
}