Merge branch 'update-service-files-of-openrc-and-systemd-to-new-recommended-paths...
[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 Pleroma.{Upload, Uploaders.Local}
8 alias Mix.Tasks.Pleroma.Common
9 require Logger
10
11 @log_every 50
12
13 @shortdoc "Migrates uploads from local to remote storage"
14 @moduledoc """
15 Manages uploads
16
17 ## Migrate uploads from local to remote storage
18 mix pleroma.uploads migrate_local TARGET_UPLOADER [OPTIONS...]
19 Options:
20 - `--delete` - delete local uploads after migrating them to the target uploader
21
22
23 A list of avalible uploaders can be seen in config.exs
24 """
25 def run(["migrate_local", target_uploader | args]) do
26 delete? = Enum.member?(args, "--delete")
27 Common.start_pleroma()
28 local_path = Pleroma.Config.get!([Local, :uploads])
29 uploader = Module.concat(Pleroma.Uploaders, target_uploader)
30
31 unless Code.ensure_loaded?(uploader) do
32 raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
33 end
34
35 target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
36
37 unless target_enabled? do
38 Pleroma.Config.put([Upload, :uploader], uploader)
39 end
40
41 Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
42
43 if delete? do
44 Mix.shell().info(
45 "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
46 )
47
48 :timer.sleep(:timer.seconds(5))
49 end
50
51 uploads =
52 File.ls!(local_path)
53 |> Enum.map(fn id ->
54 root_path = Path.join(local_path, id)
55
56 cond do
57 File.dir?(root_path) ->
58 files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
59
60 case List.first(files) do
61 {id, file, path} ->
62 {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
63 root_path}
64
65 _ ->
66 nil
67 end
68
69 File.exists?(root_path) ->
70 file = Path.basename(id)
71 hash = Path.rootname(id)
72 {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
73
74 true ->
75 nil
76 end
77 end)
78 |> Enum.filter(& &1)
79
80 total_count = length(uploads)
81 Mix.shell().info("Found #{total_count} uploads")
82
83 uploads
84 |> Task.async_stream(
85 fn {upload, root_path} ->
86 case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
87 {:ok, _} ->
88 if delete?, do: File.rm_rf!(root_path)
89 Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
90 :ok
91
92 error ->
93 Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
94 end
95 end,
96 timeout: 150_000
97 )
98 |> Stream.chunk_every(@log_every)
99 |> Enum.reduce(0, fn done, count ->
100 count = count + length(done)
101 Mix.shell().info("Uploaded #{count}/#{total_count} files")
102 count
103 end)
104
105 Mix.shell().info("Done!")
106 end
107 end