1 defmodule Mix.Tasks.MigrateLocalUploads do
4 alias Pleroma.{Upload, Uploaders.Local, Uploaders.S3}
8 @shortdoc "Migrate uploads from local to remote storage"
10 def run([target_uploader | args]) do
11 delete? = Enum.member?(args, "--delete")
12 Application.ensure_all_started(:pleroma)
14 local_path = Pleroma.Config.get!([Local, :uploads])
15 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
17 unless Code.ensure_loaded?(uploader) do
18 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
21 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
23 unless target_enabled? do
24 Pleroma.Config.put([Upload, :uploader], uploader)
27 Logger.info("Migrating files from local #{local_path} to #{to_string(uploader)}")
31 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
34 :timer.sleep(:timer.seconds(5))
40 root_path = Path.join(local_path, id)
43 File.dir?(root_path) ->
44 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
46 case List.first(files) do
48 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
55 File.exists?(root_path) ->
56 file = Path.basename(id)
57 [hash, ext] = String.split(id, ".")
58 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
66 total_count = length(uploads)
67 Logger.info("Found #{total_count} uploads")
71 fn {upload, root_path} ->
72 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
74 if delete?, do: File.rm_rf!(root_path)
75 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
79 Logger.error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
84 |> Stream.chunk_every(@log_every)
85 |> Enum.reduce(0, fn done, count ->
86 count = count + length(done)
87 Logger.info("Uploaded #{count}/#{total_count} files")
95 Logger.error("Usage: migrate_local_uploads S3|Swift [--delete]")