Can now update archive from url

This commit is contained in:
towards-a-new-leftypol 2024-04-09 21:01:06 -04:00
parent fc321d8531
commit 93d789fa65
3 changed files with 21 additions and 7 deletions

View File

@ -378,7 +378,6 @@ processFiles settings fgs tuples = do -- perfect just means that our posts have
to_insert_ <- mapM ensureAttachmentExists to_insert to_insert_ <- mapM ensureAttachmentExists to_insert
let to_insert_exist = catMaybes to_insert_ let to_insert_exist = catMaybes to_insert_
-- to_insert_exist <- filterM attachmentFileExists to_insert
with_hashes <- mapM computeAttachmentHash to_insert_exist with_hashes <- mapM computeAttachmentHash to_insert_exist
@ -391,7 +390,7 @@ processFiles settings fgs tuples = do -- perfect just means that our posts have
Right saved -> do Right saved -> do
putStrLn $ "Saved " ++ (show $ length saved) ++ " attachments!" putStrLn $ "Saved " ++ (show $ length saved) ++ " attachments!"
mapM_ (copyOrMoveFiles settings fgs) attachments_on_board mapM_ (copyOrMoveFiles settings fgs) to_insert_exist
where where
ensureAttachmentExists :: Details -> IO (Maybe Details) ensureAttachmentExists :: Details -> IO (Maybe Details)

View File

@ -9,7 +9,7 @@ import Data.Aeson (decode)
import System.FilePath ((</>)) import System.FilePath ((</>))
import Control.Concurrent.Async (mapConcurrently) import Control.Concurrent.Async (mapConcurrently)
import Data.Aeson (FromJSON) import Data.Aeson (FromJSON)
import System.Directory (createDirectoryIfMissing, renameFile) import System.Directory (createDirectoryIfMissing, removeFile)
import qualified SitesType as Sites import qualified SitesType as Sites
import Common.Server.ConsumerSettings import Common.Server.ConsumerSettings
@ -54,6 +54,13 @@ getSettings = do
exitFailure exitFailure
Just settings -> return settings Just settings -> return settings
-- Move a file by reading, writing, and then deleting the original
moveFile :: FilePath -> FilePath -> IO ()
moveFile src dst =
B.readFile src >>= B.writeFile dst >> removeFile src
httpFileGetters :: JSONSettings -> FileGetters httpFileGetters :: JSONSettings -> FileGetters
httpFileGetters settings = FileGetters httpFileGetters settings = FileGetters
{ getJSONCatalog = httpGetJSON { getJSONCatalog = httpGetJSON
@ -66,12 +73,14 @@ httpFileGetters settings = FileGetters
thumbpath <- Client.getFile (At.thumbnail_path paths) thumbpath <- Client.getFile (At.thumbnail_path paths)
return $ filepath >>= \fp -> return $ filepath >>= \fp ->
thumbpath >>= \tp -> thumbpath >>= \tp -> do
return (At.Paths fp tp) return (At.Paths fp tp)
, copyOrMove = \common_dest (src, dest) (thumb_src, thumb_dest) -> do , copyOrMove = \common_dest (src, dest) (thumb_src, thumb_dest) -> do
putStrLn $ "Copy Or Move (Move) src: " ++ src ++ " dest: " ++ dest
createDirectoryIfMissing True common_dest createDirectoryIfMissing True common_dest
renameFile src dest moveFile src dest
renameFile thumb_src thumb_dest moveFile thumb_src thumb_dest
} }
httpGetJSON :: (FromJSON a) => Sites.Site -> String -> IO (Either String a) httpGetJSON :: (FromJSON a) => Sites.Site -> String -> IO (Either String a)

View File

@ -39,6 +39,7 @@ import Data.Aeson
) )
import GHC.Generics import GHC.Generics
import System.IO.Temp (openBinaryTempFile, getCanonicalTemporaryDirectory) import System.IO.Temp (openBinaryTempFile, getCanonicalTemporaryDirectory)
import System.IO (hClose)
import qualified Common.Server.JSONSettings as T import qualified Common.Server.JSONSettings as T
import qualified SitesType as Sites import qualified SitesType as Sites
@ -226,15 +227,20 @@ getJSON url = get_ url [] >>= return . eitherDecodeResponse
getFile :: String -> IO (Maybe String) getFile :: String -> IO (Maybe String)
getFile url = do getFile url = do
putStrLn $ "getFile " ++ url
result <- get_ url [] result <- get_ url []
case result of case result of
Left (err :: HttpError) -> do Left (err :: HttpError) -> do
putStrLn $ "getFile " ++ url ++ " Error!"
putStrLn $ show err putStrLn $ show err
return Nothing return Nothing
Right lbs -> do Right lbs -> do
putStrLn $ "getFile " ++ url ++ " SUCCESS!"
tmp_root <- getCanonicalTemporaryDirectory tmp_root <- getCanonicalTemporaryDirectory
(tmp_filepath, tmp_filehandle) <- openBinaryTempFile tmp_root "chan.attachment" (tmp_filepath, tmp_filehandle) <- openBinaryTempFile tmp_root "chan.attachment"
LBS.writeFile tmp_filepath lbs putStrLn $ "Created " ++ tmp_filepath
putStrLn $ "Writing attachment..."
LBS.hPut tmp_filehandle lbs LBS.hPut tmp_filehandle lbs
hClose tmp_filehandle
return $ Just tmp_filepath return $ Just tmp_filepath