1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Uploads do
7 alias Mix.Tasks.Pleroma.Common
9 alias Pleroma.Uploaders.Local
14 @shortdoc "Migrates uploads from local to remote storage"
18 ## Migrate uploads from local to remote storage
19 mix pleroma.uploads migrate_local TARGET_UPLOADER [OPTIONS...]
21 - `--delete` - delete local uploads after migrating them to the target uploader
24 A list of available uploaders can be seen in config.exs
26 def run(["migrate_local", target_uploader | args]) do
27 delete? = Enum.member?(args, "--delete")
28 Common.start_pleroma()
29 local_path = Pleroma.Config.get!([Local, :uploads])
30 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
32 unless Code.ensure_loaded?(uploader) do
33 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
36 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
38 unless target_enabled? do
39 Pleroma.Config.put([Upload, :uploader], uploader)
42 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
46 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
49 :timer.sleep(:timer.seconds(5))
55 root_path = Path.join(local_path, id)
58 File.dir?(root_path) ->
59 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
61 case List.first(files) do
63 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
70 File.exists?(root_path) ->
71 file = Path.basename(id)
72 hash = Path.rootname(id)
73 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
81 total_count = length(uploads)
82 Mix.shell().info("Found #{total_count} uploads")
86 fn {upload, root_path} ->
87 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
89 if delete?, do: File.rm_rf!(root_path)
90 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
94 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
99 |> Stream.chunk_every(@log_every)
100 # credo:disable-for-next-line Credo.Check.Warning.UnusedEnumOperation
101 |> Enum.reduce(0, fn done, count ->
102 count = count + length(done)
103 Mix.shell().info("Uploaded #{count}/#{total_count} files")
107 Mix.shell().info("Done!")