1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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"
18 ## Migrate uploads from local to remote storage
19 mix pleroma.uploads migrate_local TARGET_UPLOADER [OPTIONS...]
21 - `--delete` - delete local uploads after migrating them to the target uploader
23 A list of available uploaders can be seen in config.exs
25 def run(["migrate_local", target_uploader | args]) do
26 delete? = Enum.member?(args, "--delete")
28 local_path = Pleroma.Config.get!([Local, :uploads])
29 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
31 unless Code.ensure_loaded?(uploader) do
32 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
35 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
37 unless target_enabled? do
38 Pleroma.Config.put([Upload, :uploader], uploader)
41 shell_info("Migrating files from local #{local_path} to #{to_string(uploader)}")
45 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
48 :timer.sleep(:timer.seconds(5))
54 root_path = Path.join(local_path, id)
57 File.dir?(root_path) ->
58 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
60 case List.first(files) do
62 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
69 File.exists?(root_path) ->
70 file = Path.basename(id)
71 hash = Path.rootname(id)
72 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
80 total_count = length(uploads)
81 shell_info("Found #{total_count} uploads")
85 fn {upload, root_path} ->
86 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
88 if delete?, do: File.rm_rf!(root_path)
89 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
93 shell_error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
98 |> Stream.chunk_every(@log_every)
99 # credo:disable-for-next-line Credo.Check.Warning.UnusedEnumOperation
100 |> Enum.reduce(0, fn done, count ->
101 count = count + length(done)
102 shell_info("Uploaded #{count}/#{total_count} files")