getFilesForAssetGetR :: AssetId -> Handler RepPlain
getFilesForAssetGetR key = do
  asset <- runDB $ get404 key
  now <- liftIO getCurrentTime
  now'posix <- liftIO getPOSIXTime

  files <- runDB $ selectList [FileAsset ==. key] [Asc FileId]

  let lt2lbs t = t2lbs . Data.Text.Lazy.toStrict $ t 
      t2lbs t = LBS.fromChunks . singletonList . Data.Text.Encoding.encodeUtf8 $ t
      singletonList x = [x]

      readFileFromDisk (Entity _ file) = LBS.readFile (filePath file)
      fileEntry (Entity fid file) = 
        let name = Data.Text.Format.format "{}/{}" ((Shown fid), (fileOriginalName file))
            -- fixme: error/warning for too big files 
            size = fromIntegral . fileSize $ file
        
            e = Entry { eRelativePath = read . show $ name
                       , eCompressionMethod = NoCompression
                       , eLastModified = round now'posix
                       , eCRC32 = fileCrc file
                       , eCompressedSize = size
                       , eUncompressedSize = size
                       , eExtraField = LBS.empty
                       , eFileComment = t2lbs $ unTextarea $ fileComment file
                       , eInternalFileAttributes = 0
                       , eExternalFileAttributes = 0
                       }
                        in e

  files'lazy <- liftIO $ mapM readFileFromDisk files
  let entries = map fileEntry files

  let comment = Data.Text.Format.format "File bundle for asset {} - {}" (Shown key, assetName asset)
      archive =
         Archive
         { zComment = lt2lbs comment
         , zSignature = Nothing
         , zEntries = entries
         }

--  let now'fmt = Data.Time.formatTime System.Locale.defaultTimeLocale "%s" now
--      fname = Data.Text.Format.format "asset_{}_file_bundle_{}.zip" (Shown key, now'fmt)
--      cont'disp = Data.Text.Encoding.encodeUtf8 $ Data.Text.Format.format "attachment; filename=\"{}\";" [fname]
  setHeader "Content-Disposition" "attachment; filename=archive-all.zip;" -- (Data.Text.Lazy.toStrict cont'disp)
  setHeader "X-Content-Type-Options" "nosniff"

--  let h1 = ("Content-Disposition","attachment; filename=archive-all.zip;") -- fixme: provide filename different for each asset
--      h2 = ("X-Content-Type-Options","nosniff")
--      -- h3 = ("Content-Length","") -- fixme: calculate exact actual content length.
--  
--  sendWaiResponse (responseLBS ok200 [h1,h2] (fromArchive files'lazy archive))

  let content = ContentBuilder (Builder.fromLazyByteString (fromArchive files'lazy archive)) Nothing -- fixme: calculate response length
  return (RepPlain content)
  

-- delete
getFilesForAssetGetR :: AssetId -> Handler RepPlain
getFilesForAssetGetR key = do
  asset <- runDB $ get404 key
  now <- liftIO getCurrentTime
  now'posix <- liftIO getPOSIXTime

  files <- runDB $ selectList [FileAsset ==. key] [Asc FileId]

  let lt2lbs t = t2lbs . Data.Text.Lazy.toStrict $ t 
      t2lbs t = LBS.fromChunks . singletonList . Data.Text.Encoding.encodeUtf8 $ t
      singletonList x = [x]

      readFileFromDisk (Entity _ file) = LBS.readFile (filePath file)
      fileEntry (Entity fid file) = 
        let name = Data.Text.Format.format "{}/{}" ((Shown fid), (fileOriginalName file))
            -- fixme: error/warning for too big files 
            size = fromIntegral . fileSize $ file
        
            e = Entry { eRelativePath = read . show $ name
                       , eCompressionMethod = NoCompression
                       , eLastModified = round now'posix
                       , eCRC32 = fileCrc file
                       , eCompressedSize = size
                       , eUncompressedSize = size
                       , eExtraField = LBS.empty
                       , eFileComment = t2lbs $ unTextarea $ fileComment file
                       , eInternalFileAttributes = 0
                       , eExternalFileAttributes = 0
                       }
                        in e

  files'lazy <- liftIO $ mapM readFileFromDisk files
  let entries = map fileEntry files

  let comment = Data.Text.Format.format "File bundle for asset {} - {}" (Shown key, assetName asset)
      archive =
         Archive
         { zComment = lt2lbs comment
         , zSignature = Nothing
         , zEntries = entries
         }

--  let now'fmt = Data.Time.formatTime System.Locale.defaultTimeLocale "%s" now
--      fname = Data.Text.Format.format "asset_{}_file_bundle_{}.zip" (Shown key, now'fmt)
--      cont'disp = Data.Text.Encoding.encodeUtf8 $ Data.Text.Format.format "attachment; filename=\"{}\";" [fname]
  setHeader "Content-Disposition" "attachment; filename=archive-all.zip;" -- (Data.Text.Lazy.toStrict cont'disp)
  setHeader "X-Content-Type-Options" "nosniff"

--  let h1 = ("Content-Disposition","attachment; filename=archive-all.zip;") -- fixme: provide filename different for each asset
--      h2 = ("X-Content-Type-Options","nosniff")
--      -- h3 = ("Content-Length","") -- fixme: calculate exact actual content length.
--  
--  sendWaiResponse (responseLBS ok200 [h1,h2] (fromArchive files'lazy archive))

  let content = ContentBuilder (Builder.fromLazyByteString (fromArchive files'lazy archive)) Nothing -- fixme: calculate response length
  return (RepPlain content)
  

-- delete
getFilesForAssetGetR key = do
  asset <- runDB $ get404 key
  now <- liftIO getCurrentTime
  now'posix <- liftIO getPOSIXTime

  files <- runDB $ selectList [FileAsset ==. key] [Asc FileId]

  let lt2lbs t = t2lbs . Data.Text.Lazy.toStrict $ t 
      t2lbs t = LBS.fromChunks . singletonList . Data.Text.Encoding.encodeUtf8 $ t
      singletonList x = [x]

      readFileFromDisk (Entity _ file) = LBS.readFile (filePath file)
      fileEntry (Entity fid file) = 
        let name = Data.Text.Format.format "{}/{}" ((Shown fid), (fileOriginalName file))
            -- fixme: error/warning for too big files 
            size = fromIntegral . fileSize $ file
        
            e = Entry { eRelativePath = read . show $ name
                       , eCompressionMethod = NoCompression
                       , eLastModified = round now'posix
                       , eCRC32 = fileCrc file
                       , eCompressedSize = size
                       , eUncompressedSize = size
                       , eExtraField = LBS.empty
                       , eFileComment = t2lbs $ unTextarea $ fileComment file
                       , eInternalFileAttributes = 0
                       , eExternalFileAttributes = 0
                       }
                        in e

  files'lazy <- liftIO $ mapM readFileFromDisk files
  let entries = map fileEntry files

  let comment = Data.Text.Format.format "File bundle for asset {} - {}" (Shown key, assetName asset)
      archive =
         Archive
         { zComment = lt2lbs comment
         , zSignature = Nothing
         , zEntries = entries
         }

--  let now'fmt = Data.Time.formatTime System.Locale.defaultTimeLocale "%s" now
--      fname = Data.Text.Format.format "asset_{}_file_bundle_{}.zip" (Shown key, now'fmt)
--      cont'disp = Data.Text.Encoding.encodeUtf8 $ Data.Text.Format.format "attachment; filename=\"{}\";" [fname]
  setHeader "Content-Disposition" "attachment; filename=archive-all.zip;" -- (Data.Text.Lazy.toStrict cont'disp)
  setHeader "X-Content-Type-Options" "nosniff"

--  let h1 = ("Content-Disposition","attachment; filename=archive-all.zip;") -- fixme: provide filename different for each asset
--      h2 = ("X-Content-Type-Options","nosniff")
--      -- h3 = ("Content-Length","") -- fixme: calculate exact actual content length.
--  
--  sendWaiResponse (responseLBS ok200 [h1,h2] (fromArchive files'lazy archive))

  let content = ContentBuilder (Builder.fromLazyByteString (fromArchive files'lazy archive)) Nothing -- fixme: calculate response length
  return (RepPlain content)
  

-- delete