core, ui: clean media filename on forwards (#5522)

* core, ui: clean media name on forwards

* fix forward tests for new jpg files format
This commit is contained in:
Diogo
2025-01-13 16:51:15 +00:00
committed by GitHub
parent bd396cb4d6
commit 0d44e9f0f5
2 changed files with 36 additions and 15 deletions
+22 -2
View File
@@ -129,7 +129,7 @@ import Simplex.Messaging.Version
import Simplex.RemoteControl.Invitation (RCInvitation (..), RCSignedInvitation (..))
import Simplex.RemoteControl.Types (RCCtrlAddress (..))
import System.Exit (ExitCode, exitSuccess)
import System.FilePath (takeFileName, (</>))
import System.FilePath (takeExtension, takeFileName, (</>))
import qualified System.FilePath as FP
import System.IO (Handle, IOMode (..), SeekMode (..), hFlush)
import System.Random (randomRIO)
@@ -292,6 +292,15 @@ fixedImagePreview = ImageData "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEA
smallGroupsRcptsMemLimit :: Int
smallGroupsRcptsMemLimit = 20
imageFilePrefix :: String
imageFilePrefix = "IMG_"
voiceFilePrefix :: String
voiceFilePrefix = "voice_"
videoFilePrefix :: String
videoFilePrefix = "video_"
logCfg :: LogConfig
logCfg = LogConfig {lc_file = Nothing, lc_stderr = True}
@@ -1237,7 +1246,12 @@ processChatCommand' vr = \case
ifM
(doesFileExist fsFromPath)
( do
fsNewPath <- liftIO $ filesFolder `uniqueCombine` fileName
newFileName <- case mc of
MCImage {} -> liftIO $ generateNewFileName fileName imageFilePrefix
MCVoice {} -> liftIO $ generateNewFileName fileName voiceFilePrefix
MCVideo {} -> liftIO $ generateNewFileName fileName videoFilePrefix
_ -> pure fileName
fsNewPath <- liftIO $ filesFolder `uniqueCombine` newFileName
liftIO $ B.writeFile fsNewPath "" -- create empty file
encrypt <- chatReadVar encryptLocalFiles
cfArgs <- if encrypt then Just <$> (atomically . CF.randomArgs =<< asks random) else pure Nothing
@@ -1274,6 +1288,12 @@ processChatCommand' vr = \case
when (B.length ch /= chSize') $ throwError $ CF.FTCEFileIOError "encrypting file: unexpected EOF"
liftIO . CF.hPut w $ LB.fromStrict ch
when (size' > 0) $ copyChunks r w size'
generateNewFileName :: String -> String -> IO String
generateNewFileName fileName prefix = do
currentDate <- liftIO getCurrentTime
let formattedDate = formatTime defaultTimeLocale "%Y%m%d_%H%M%S" currentDate
let ext = takeExtension fileName
pure $ prefix <> formattedDate <> ext
APIUserRead userId -> withUserId userId $ \user -> withFastStore' (`setUserChatsRead` user) >> ok user
UserRead -> withUser $ \User {userId} -> processChatCommand $ APIUserRead userId
APIChatRead chatRef@(ChatRef cType chatId) -> withUser $ \_ -> case cType of
+14 -13
View File
@@ -9,9 +9,9 @@ import Control.Concurrent (threadDelay)
import qualified Data.ByteString.Char8 as B
import Data.List (intercalate)
import qualified Data.Text as T
import System.Directory (copyFile, doesFileExist, removeFile)
import Simplex.Chat (fixedImagePreview)
import Simplex.Chat.Types (ImageData (..))
import System.Directory (copyFile, doesFileExist, removeFile)
import Test.Hspec hiding (it)
chatForwardTests :: SpecWith FilePath
@@ -740,7 +740,7 @@ testMultiForwardFiles =
-- IDs to forward
let msgId1 = (read msgIdZero :: Int) + 1
msgIds = intercalate "," $ map (show . (msgId1 +)) [0..5]
msgIds = intercalate "," $ map (show . (msgId1 +)) [0 .. 5]
bob ##> ("/_forward plan @2 " <> msgIds)
bob <## "Files can be received: 1, 2, 3, 4"
bob <## "5 message(s) out of 6 can be forwarded"
@@ -785,8 +785,9 @@ testMultiForwardFiles =
bob <## " message without file"
bob <# "@cath <- @alice"
bob <## " test_1.jpg"
bob <# "/f @cath test_1.jpg"
jpgFileName <- T.unpack . T.strip . T.pack <$> getTermLine bob
bob <# ("/f @cath " <> jpgFileName)
bob <## "use /fc 5 to cancel sending"
bob <# "@cath <- @alice"
@@ -808,8 +809,8 @@ testMultiForwardFiles =
cath <## " message without file"
cath <# "bob> -> forwarded"
cath <## " test_1.jpg"
cath <# "bob> sends file test_1.jpg (136.5 KiB / 139737 bytes)"
cath <## (" " <> jpgFileName)
cath <# ("bob> sends file " <> jpgFileName <> " (136.5 KiB / 139737 bytes)")
cath <## "use /fr 1 [<dir>/ | <path>] to receive it"
cath <# "bob> -> forwarded"
@@ -824,15 +825,15 @@ testMultiForwardFiles =
cath <## ""
-- file transfer
bob <## "completed uploading file 5 (test_1.jpg) for cath"
bob <## ("completed uploading file 5 (" <> jpgFileName <> ") for cath")
bob <## "completed uploading file 6 (test_1.pdf) for cath"
cath ##> "/fr 1"
cath
<### [ "saving file 1 from bob to test_1.jpg",
"started receiving file 1 (test_1.jpg) from bob"
<### [ ConsoleString $ "saving file 1 from bob to " <> jpgFileName,
ConsoleString $ "started receiving file 1 (" <> jpgFileName <> ") from bob"
]
cath <## "completed receiving file 1 (test_1.jpg) from bob"
cath <## ("completed receiving file 1 (" <> jpgFileName <> ") from bob")
cath ##> "/fr 2"
cath
@@ -841,9 +842,9 @@ testMultiForwardFiles =
]
cath <## "completed receiving file 2 (test_1.pdf) from bob"
src1B <- B.readFile "./tests/tmp/bob_app_files/test_1.jpg"
src1B <- B.readFile ("./tests/tmp/bob_app_files/" <> jpgFileName)
src1B `shouldBe` dest1
dest1C <- B.readFile "./tests/tmp/cath_app_files/test_1.jpg"
dest1C <- B.readFile ("./tests/tmp/cath_app_files/" <> jpgFileName)
dest1C `shouldBe` src1B
src2B <- B.readFile "./tests/tmp/bob_app_files/test_1.pdf"
@@ -886,5 +887,5 @@ testMultiForwardFiles =
checkActionDeletesFile "./tests/tmp/bob_app_files/test.jpg" $ do
bob ##> "/clear alice"
bob <## "alice: all messages are removed locally ONLY"
fwdFileExists <- doesFileExist "./tests/tmp/bob_app_files/test_1.jpg"
fwdFileExists <- doesFileExist ("./tests/tmp/bob_app_files/" <> jpgFileName)
fwdFileExists `shouldBe` True