1 defmodule Mix.Tasks.Pleroma.Uploads do
3 alias Pleroma.{Upload, Uploaders.Local}
4 alias Mix.Tasks.Pleroma.Common
9 @shortdoc "Migrates uploads from local to remote storage"
13 ## Migrate uploads from local to remote storage
14 mix pleroma.uploads migrate_local TARGET_UPLOADER [OPTIONS...]
16 - `--delete` - delete local uploads after migrating them to the target uploader
19 A list of avalible uploaders can be seen in config.exs
21 def run(["migrate_local", target_uploader | args]) do
22 delete? = Enum.member?(args, "--delete")
23 Common.start_pleroma()
24 local_path = Pleroma.Config.get!([Local, :uploads])
25 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
27 unless Code.ensure_loaded?(uploader) do
28 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
31 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
33 unless target_enabled? do
34 Pleroma.Config.put([Upload, :uploader], uploader)
37 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
41 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
44 :timer.sleep(:timer.seconds(5))
50 root_path = Path.join(local_path, id)
53 File.dir?(root_path) ->
54 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
56 case List.first(files) do
58 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
65 File.exists?(root_path) ->
66 file = Path.basename(id)
67 hash = Path.rootname(id)
68 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
76 total_count = length(uploads)
77 Mix.shell().info("Found #{total_count} uploads")
81 fn {upload, root_path} ->
82 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
84 if delete?, do: File.rm_rf!(root_path)
85 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
89 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
94 |> Stream.chunk_every(@log_every)
95 |> Enum.reduce(0, fn done, count ->
96 count = count + length(done)
97 Mix.shell().info("Uploaded #{count}/#{total_count} files")
101 Mix.shell().info("Done!")