Browse Source

video fix

Viktoriia 4 months ago
parent
commit
2eaf9775d2

+ 2 - 2
src/screens/InAppScreens/MessagesScreen/ChatScreen/index.tsx

@@ -432,7 +432,7 @@ const ChatScreen = ({ route }: { route: any }) => {
       const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
 
       const fileExists = await FileSystem.getInfoAsync(fileUri);
-      if (fileExists.exists) {
+      if (fileExists.exists && fileExists.size > 1024) {
         await FileViewer.open(fileUri, {
           showOpenWithDialog: true,
           showAppsSuggestions: true
@@ -1470,7 +1470,7 @@ const ChatScreen = ({ route }: { route: any }) => {
     const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
 
     const fileExists = await FileSystem.getInfoAsync(fileUri);
-    if (fileExists.exists) {
+    if (fileExists.exists && fileExists.size > 1024) {
       setSelectedMedia(fileUri);
 
       return;

+ 31 - 3
src/screens/InAppScreens/MessagesScreen/Components/renderMessageVideo.tsx

@@ -28,6 +28,8 @@ const RenderMessageVideo = ({
   const [isBuffering, setIsBuffering] = useState(true);
   const [videoUri, setVideoUri] = useState<string | null>(null);
   const [isVideoLoaded, setIsVideoLoaded] = useState(false);
+  const [retryCount, setRetryCount] = useState(0);
+  const MAX_RETRY = 3;
 
   const downloadVideo = async (videoUrl: string) => {
     if (!videoUrl.startsWith('https')) {
@@ -46,9 +48,20 @@ const RenderMessageVideo = ({
 
       const videoExists = await FileSystem.getInfoAsync(videoPath);
       if (videoExists.exists) {
-        setVideoUri(videoPath);
-        setIsVideoLoaded(true);
-        return videoPath;
+        if (videoExists.size < 1024) {
+          await FileSystem.deleteAsync(videoPath, { idempotent: true });
+        } else {
+          try {
+            await FileSystem.readAsStringAsync(videoPath, {
+              encoding: FileSystem.EncodingType.Base64
+            });
+            setVideoUri(videoPath);
+            setIsVideoLoaded(true);
+            return videoPath;
+          } catch (e) {
+            await FileSystem.deleteAsync(videoPath, { idempotent: true });
+          }
+        }
       }
 
       const downloadResult = await FileSystem.downloadAsync(videoUrl, videoPath, {
@@ -118,6 +131,21 @@ const RenderMessageVideo = ({
           posterStyle={{ resizeMode: 'cover', width: '100%', height: '100%' }}
           usePoster={true}
           posterSource={{ uri: API_HOST + currentMessage.attachment.attachment_small_url }}
+          onError={async () => {
+            if (retryCount >= MAX_RETRY) {
+              return;
+            }
+
+            if (videoUri) {
+              await FileSystem.deleteAsync(videoUri, { idempotent: true });
+
+              const newUri = await downloadVideo(currentMessage.video);
+              if (newUri) {
+                setVideoUri(newUri);
+                setRetryCount(retryCount + 1);
+              }
+            }
+          }}
         />
       ) : null}
 

+ 2 - 2
src/screens/InAppScreens/MessagesScreen/GroupChatScreen/index.tsx

@@ -440,7 +440,7 @@ const GroupChatScreen = ({ route }: { route: any }) => {
       const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
 
       const fileExists = await FileSystem.getInfoAsync(fileUri);
-      if (fileExists.exists) {
+      if (fileExists.exists && fileExists.size > 1024) {
         await FileViewer.open(fileUri, {
           showOpenWithDialog: true,
           showAppsSuggestions: true
@@ -1536,7 +1536,7 @@ const GroupChatScreen = ({ route }: { route: any }) => {
     const fileUri = `${CACHED_ATTACHMENTS_DIR}${fileName}`;
 
     const fileExists = await FileSystem.getInfoAsync(fileUri);
-    if (fileExists.exists) {
+    if (fileExists.exists && fileExists.size > 1024) {
       setSelectedMedia(fileUri);
 
       return;