2018-12-23 13:05:55 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:05:55 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-07 07:13:57 -07:00
|
|
|
defmodule Mix.Tasks.Pleroma.Uploads do
|
|
|
|
use Mix.Task
|
2019-06-19 17:05:19 -06:00
|
|
|
import Mix.Pleroma
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Upload
|
|
|
|
alias Pleroma.Uploaders.Local
|
2018-12-07 07:13:57 -07:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@log_every 50
|
2018-12-10 11:33:36 -07:00
|
|
|
|
2018-12-09 22:03:17 -07:00
|
|
|
@shortdoc "Migrates uploads from local to remote storage"
|
2019-10-03 04:59:49 -06:00
|
|
|
@moduledoc File.read!("docs/administration/CLI_tasks/uploads.md")
|
2018-12-10 11:33:36 -07:00
|
|
|
|
2018-12-07 07:13:57 -07:00
|
|
|
def run(["migrate_local", target_uploader | args]) do
|
|
|
|
delete? = Enum.member?(args, "--delete")
|
2019-06-19 17:05:19 -06:00
|
|
|
start_pleroma()
|
2018-12-07 07:13:57 -07:00
|
|
|
local_path = Pleroma.Config.get!([Local, :uploads])
|
|
|
|
uploader = Module.concat(Pleroma.Uploaders, target_uploader)
|
|
|
|
|
|
|
|
unless Code.ensure_loaded?(uploader) do
|
|
|
|
raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
|
|
|
|
end
|
|
|
|
|
|
|
|
target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
|
|
|
|
|
|
|
|
unless target_enabled? do
|
|
|
|
Pleroma.Config.put([Upload, :uploader], uploader)
|
|
|
|
end
|
|
|
|
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_info("Migrating files from local #{local_path} to #{to_string(uploader)}")
|
2018-12-07 07:13:57 -07:00
|
|
|
|
|
|
|
if delete? do
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_info(
|
2018-12-07 07:13:57 -07:00
|
|
|
"Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
|
|
|
|
)
|
|
|
|
|
|
|
|
:timer.sleep(:timer.seconds(5))
|
|
|
|
end
|
|
|
|
|
|
|
|
uploads =
|
|
|
|
File.ls!(local_path)
|
|
|
|
|> Enum.map(fn id ->
|
|
|
|
root_path = Path.join(local_path, id)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
File.dir?(root_path) ->
|
|
|
|
files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
|
|
|
|
|
|
|
|
case List.first(files) do
|
|
|
|
{id, file, path} ->
|
|
|
|
{%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
|
|
|
|
root_path}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
File.exists?(root_path) ->
|
|
|
|
file = Path.basename(id)
|
2018-12-09 23:39:57 -07:00
|
|
|
hash = Path.rootname(id)
|
2018-12-07 07:13:57 -07:00
|
|
|
{%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
|
|
|
|
|
|
|
|
true ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|
|
|
|
total_count = length(uploads)
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_info("Found #{total_count} uploads")
|
2018-12-07 07:13:57 -07:00
|
|
|
|
|
|
|
uploads
|
|
|
|
|> Task.async_stream(
|
|
|
|
fn {upload, root_path} ->
|
|
|
|
case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
|
|
|
|
{:ok, _} ->
|
|
|
|
if delete?, do: File.rm_rf!(root_path)
|
|
|
|
Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
|
|
|
|
:ok
|
|
|
|
|
|
|
|
error ->
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
|
2018-12-07 07:13:57 -07:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
timeout: 150_000
|
|
|
|
)
|
|
|
|
|> Stream.chunk_every(@log_every)
|
2019-02-09 08:30:42 -07:00
|
|
|
# credo:disable-for-next-line Credo.Check.Warning.UnusedEnumOperation
|
2018-12-07 07:13:57 -07:00
|
|
|
|> Enum.reduce(0, fn done, count ->
|
|
|
|
count = count + length(done)
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_info("Uploaded #{count}/#{total_count} files")
|
2018-12-07 07:13:57 -07:00
|
|
|
count
|
|
|
|
end)
|
|
|
|
|
2019-06-19 17:05:19 -06:00
|
|
|
shell_info("Done!")
|
2018-12-07 07:13:57 -07:00
|
|
|
end
|
|
|
|
end
|