1 defmodule Mix.Tasks.Pleroma.Uploads do
3 alias Pleroma.{Upload, Uploaders.Local}
4 alias Mix.Tasks.Pleroma.Common
8 @shortdoc "Migrate uploads from local to remote storage"
11 ## Migrate uploads from local to remote storage
14 def run(["migrate_local", target_uploader | args]) do
15 delete? = Enum.member?(args, "--delete")
16 Common.start_pleroma()
17 local_path = Pleroma.Config.get!([Local, :uploads])
18 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
20 unless Code.ensure_loaded?(uploader) do
21 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
24 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
26 unless target_enabled? do
27 Pleroma.Config.put([Upload, :uploader], uploader)
30 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
34 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
37 :timer.sleep(:timer.seconds(5))
43 root_path = Path.join(local_path, id)
46 File.dir?(root_path) ->
47 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
49 case List.first(files) do
51 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
58 File.exists?(root_path) ->
59 file = Path.basename(id)
60 hash = Path.rootname(id)
61 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
69 total_count = length(uploads)
70 Mix.shell().info("Found #{total_count} uploads")
74 fn {upload, root_path} ->
75 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
77 if delete?, do: File.rm_rf!(root_path)
78 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
82 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
87 |> Stream.chunk_every(@log_every)
88 |> Enum.reduce(0, fn done, count ->
89 count = count + length(done)
90 Mix.shell().info("Uploaded #{count}/#{total_count} files")
94 Mix.shell().info("Done!")