Handle errors when creating phash

This commit is contained in:
towards-a-new-leftypol 2024-04-11 13:47:44 -04:00
parent 93d789fa65
commit 359869984e
1 changed files with 31 additions and 16 deletions

View File

@ -30,6 +30,7 @@ import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8) import Data.Text.Encoding (decodeUtf8)
import Network.Mime (defaultMimeLookup) import Network.Mime (defaultMimeLookup)
import PerceptualHash (fileHash) import PerceptualHash (fileHash)
import Control.Exception.Safe (tryAny, tryAsync, SomeException, displayException)
import JSONParsing import JSONParsing
import Common.Server.JSONSettings import Common.Server.JSONSettings
@ -277,7 +278,7 @@ fileToAttachment i post file =
} }
where where
extension = JS.ext file extension = T.filter (/= '.') $ JS.ext file
thumb_extension = T.pack $ drop 1 $ takeExtension $ unpack $ JS.thumb_path file thumb_extension = T.pack $ drop 1 $ takeExtension $ unpack $ JS.thumb_path file
@ -403,8 +404,6 @@ processFiles settings fgs tuples = do -- perfect just means that our posts have
let f = At.file_path p let f = At.file_path p
putStrLn $ "Reading " ++ f putStrLn $ "Reading " ++ f
-- putStrLn $ show p
-- putStrLn $ show (q { At.sha256_hash = "undefined" })
sha256_sum <- Hash.computeSHA256 f sha256_sum <- Hash.computeSHA256 f
@ -413,20 +412,36 @@ processFiles settings fgs tuples = do -- perfect just means that our posts have
phash :: Maybe Int64 <- phash :: Maybe Int64 <-
case (At.mimetype q) `Set.member` phash_mimetypes of case (At.mimetype q) `Set.member` phash_mimetypes of
True -> do True -> do
either_phash <- fileHash f putStrLn $ "Running tryAny $ fileHash f " ++ f
case either_phash of either_exception <- tryAny $ fileHash f
Left err_str -> do putStrLn $ "Done tryAny $ fileHash f " ++ f
putStrLn $ "Failed to compute phash for file " ++ (unpack sha256_sum) ++ " " ++ f ++ " " ++ err_str
return Nothing
Right phash_w -> do
let phash_i = Words.wordToSignedInt64 phash_w
if phash_i == 0 then do case either_exception of
putStrLn $ "phash is 0 for file " ++ (unpack sha256_sum) ++ " " ++ f Left (err :: SomeException) -> do
return Nothing putStrLn $ "Error while computing the perceptual hash of file " ++ f ++ " " ++ displayException err
else do return Nothing
putStrLn $ "phash: " ++ show phash_w Right either_phash ->
return $ Just $ Words.wordToSignedInt64 phash_w case either_phash of
Left err_str -> do
putStrLn $ "Failed to compute phash for file " ++ (unpack sha256_sum) ++ " " ++ f ++ " " ++ err_str
return Nothing
Right phash_w -> do
result <- tryAsync $ do
let phash_i = Words.wordToSignedInt64 phash_w
if phash_i == 0 then do
putStrLn $ "phash is 0 for file " ++ (unpack sha256_sum) ++ " " ++ f
return Nothing
else do
putStrLn $ "phash: " ++ show phash_w
return $ Just $ Words.wordToSignedInt64 phash_w
case result of
Left (err2 :: SomeException) -> do
putStrLn $ "Error handling phash result! " ++ displayException err2
return Nothing
Right w -> return w
False -> return Nothing False -> return Nothing