1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Uploads do
9 alias Pleroma.Uploaders.Local
14 @shortdoc "Migrates uploads from local to remote storage"
15 @moduledoc File.read!("docs/docs/administration/CLI_tasks/uploads.md")
17 def run(["migrate_local", target_uploader | args]) do
18 delete? = Enum.member?(args, "--delete")
20 local_path = Pleroma.Config.get!([Local, :uploads])
21 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
23 unless Code.ensure_loaded?(uploader) do
24 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
27 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
29 unless target_enabled? do
30 Pleroma.Config.put([Upload, :uploader], uploader)
33 shell_info("Migrating files from local #{local_path} to #{to_string(uploader)}")
37 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
40 :timer.sleep(:timer.seconds(5))
46 root_path = Path.join(local_path, id)
49 File.dir?(root_path) ->
50 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
52 case List.first(files) do
54 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
61 File.exists?(root_path) ->
62 file = Path.basename(id)
63 hash = Path.rootname(id)
64 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
72 total_count = length(uploads)
73 shell_info("Found #{total_count} uploads")
77 fn {upload, root_path} ->
78 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
80 if delete?, do: File.rm_rf!(root_path)
81 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
85 shell_error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
90 |> Stream.chunk_every(@log_every)
91 # credo:disable-for-next-line Credo.Check.Warning.UnusedEnumOperation
92 |> Enum.reduce(0, fn done, count ->
93 count = count + length(done)
94 shell_info("Uploaded #{count}/#{total_count} files")