1 defmodule Mix.Tasks.Pleroma.Uploads do
4 alias Pleroma.{Upload, Uploaders.Local, Uploaders.S3}
5 alias Mix.Tasks.Pleroma.Common
9 @shortdoc "Manages uploads"
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
22 def run(["migrate_local", target_uploader | args]) do
23 delete? = Enum.member?(args, "--delete")
24 Common.start_pleroma()
25 local_path = Pleroma.Config.get!([Local, :uploads])
26 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
28 unless Code.ensure_loaded?(uploader) do
29 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
32 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
34 unless target_enabled? do
35 Pleroma.Config.put([Upload, :uploader], uploader)
38 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
42 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
45 :timer.sleep(:timer.seconds(5))
51 root_path = Path.join(local_path, id)
54 File.dir?(root_path) ->
55 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
57 case List.first(files) do
59 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
66 File.exists?(root_path) ->
67 file = Path.basename(id)
68 [hash, ext] = String.split(id, ".")
69 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
77 total_count = length(uploads)
78 Mix.shell().info("Found #{total_count} uploads")
82 fn {upload, root_path} ->
83 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
85 if delete?, do: File.rm_rf!(root_path)
86 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
90 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
95 |> Stream.chunk_every(@log_every)
96 |> Enum.reduce(0, fn done, count ->
97 count = count + length(done)
98 Mix.shell().info("Uploaded #{count}/#{total_count} files")
102 Mix.shell().info("Done!")