1 defmodule Mix.Tasks.Pleroma.Uploads do
4 alias Pleroma.{Upload, Uploaders.Local, Uploaders.S3}
5 alias Mix.Tasks.Pleroma.Common
9 @shortdoc "Migrate uploads from local to remote storage"
12 ## Migrate uploads from local to remote storage
16 def run(["migrate_local", target_uploader | args]) do
17 delete? = Enum.member?(args, "--delete")
18 Common.start_pleroma()
19 local_path = Pleroma.Config.get!([Local, :uploads])
20 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
22 unless Code.ensure_loaded?(uploader) do
23 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
26 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
28 unless target_enabled? do
29 Pleroma.Config.put([Upload, :uploader], uploader)
32 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
36 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
39 :timer.sleep(:timer.seconds(5))
45 root_path = Path.join(local_path, id)
48 File.dir?(root_path) ->
49 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
51 case List.first(files) do
53 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
60 File.exists?(root_path) ->
61 file = Path.basename(id)
62 [hash, ext] = String.split(id, ".")
63 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
71 total_count = length(uploads)
72 Mix.shell().info("Found #{total_count} uploads")
76 fn {upload, root_path} ->
77 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
79 if delete?, do: File.rm_rf!(root_path)
80 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
84 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
89 |> Stream.chunk_every(@log_every)
90 |> Enum.reduce(0, fn done, count ->
91 count = count + length(done)
92 Mix.shell().info("Uploaded #{count}/#{total_count} files")
96 Mix.shell().info("Done!")