index.ts 966 B

12345678910111213141516171819202122232425262728293031
  1. import * as FileSystem from 'expo-file-system';
  2. import { API_HOST } from 'src/constants';
  3. const downloadFlag = async (flagId: number) => {
  4. try {
  5. const flagUrl = `${API_HOST}/img/flags_new/${flagId}.png`;
  6. const fileName = `${flagId}.png`;
  7. let fileUri = `${FileSystem.documentDirectory}flags/${fileName}`;
  8. await FileSystem.downloadAsync(flagUrl, fileUri);
  9. } catch (error) {
  10. console.error('Error downloading flags: ', error);
  11. }
  12. };
  13. export const downloadFlags = async () => {
  14. const totalFlags = 635;
  15. const dirInfo = await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}flags`);
  16. if (!dirInfo.exists) {
  17. await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}flags`, { intermediates: true });
  18. for (let flagId = 1; flagId <= totalFlags; flagId++) {
  19. try {
  20. await downloadFlag(flagId);
  21. } catch (error) {
  22. console.error(`Error downloading flag ${flagId}:`, error);
  23. }
  24. }
  25. }
  26. }