9ca123a9b267fb96861cf8fc63cee4be29eb614b
[akkoma] / lib / mix / tasks / pleroma / uploads.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Uploads do
6 use Mix.Task
7 alias Mix.Tasks.Pleroma.Common
8 alias Pleroma.Upload
9 alias Pleroma.Uploaders.Local
10 require Logger
11
12 @log_every 50
13
14 @shortdoc "Migrates uploads from local to remote storage"
15 @moduledoc """
16 Manages uploads
17
18 ## Migrate uploads from local to remote storage
19 mix pleroma.uploads migrate_local TARGET_UPLOADER [OPTIONS...]
20 Options:
21 - `--delete` - delete local uploads after migrating them to the target uploader
22
23
24 A list of available uploaders can be seen in config.exs
25 """
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)
31
32 unless Code.ensure_loaded?(uploader) do
33 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
34 end
35
36 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
37
38 unless target_enabled? do
39 Pleroma.Config.put([Upload, :uploader], uploader)
40 end
41
42 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
43
44 if delete? do
45 Mix.shell().info(
46 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
47 )
48
49 :timer.sleep(:timer.seconds(5))
50 end
51
52 uploads =
53 File.ls!(local_path)
54 |> Enum.map(fn id ->
55 root_path = Path.join(local_path, id)
56
57 cond do
58 File.dir?(root_path) ->
59 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
60
61 case List.first(files) do
62 {id, file, path} ->
63 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
64 root_path}
65
66 _ ->
67 nil
68 end
69
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}
74
75 true ->
76 nil
77 end
78 end)
79 |> Enum.filter(& &1)
80
81 total_count = length(uploads)
82 Mix.shell().info("Found #{total_count} uploads")
83
84 uploads
85 |> Task.async_stream(
86 fn {upload, root_path} ->
87 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
88 {:ok, _} ->
89 if delete?, do: File.rm_rf!(root_path)
90 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
91 :ok
92
93 error ->
94 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
95 end
96 end,
97 timeout: 150_000
98 )
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")
104 count
105 end)
106
107 Mix.shell().info("Done!")
108 end
109 end