-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload_deer_file.ex
More file actions
153 lines (125 loc) · 4.38 KB
/
upload_deer_file.ex
File metadata and controls
153 lines (125 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
defmodule DeerStorage.Services.UploadDeerFile do
defstruct [
:caller_pid,
:tmp_path,
:record,
:original_filename,
:subscription,
:subscription_id,
:uploaded_by_user_id,
:id,
:kilobytes,
:mimetype
]
import Ecto.Query, warn: false
alias DeerStorage.Repo
alias DeerStorage.DeerRecords.DeerRecord
import DeerStorage.DeerRecords, only: [prepend_record_with_deer_file!: 2]
import DeerStorage.Users, only: [ensure_user_subscription_link!: 2]
import DeerStorageWeb.LiveHelpers, only: [is_expired?: 1]
def run!(pid, tmp_path, original_filename, record_id, user_id, id) do
{:ok, inside_transaction_result} =
Repo.transaction(fn ->
record =
Repo.one!(from(dr in DeerRecord, where: dr.id == ^record_id, lock: "FOR UPDATE"))
|> Repo.preload(:subscription)
assigns = %__MODULE__{
caller_pid: pid,
tmp_path: tmp_path,
record: record,
original_filename: original_filename,
subscription: record.subscription,
subscription_id: record.subscription.id,
uploaded_by_user_id: user_id,
id: id
}
assigns
|> raise_if_subscription_is_expired
|> ensure_user_subscription_link_from_assigns!
|> validate_maximum_filename_length
|> ensure_limits_for_subscription
|> copy_file!
|> detect_and_assign_mime_type
# this may be wrong if update_record raises error
|> notify_subscription_storage_cache
|> update_record
end)
GenServer.cast(
pid,
{:upload_deer_file_result, {original_filename, inside_transaction_result}}
)
after
File.rm!(tmp_path)
end
defp raise_if_subscription_is_expired(%{subscription: subscription} = assigns) do
if is_expired?(subscription), do: raise("Subscription is expired")
assigns
end
defp ensure_user_subscription_link_from_assigns!(
%{uploaded_by_user_id: user_id, subscription_id: subscription_id} = assigns
) do
ensure_user_subscription_link!(user_id, subscription_id)
assigns
end
defp validate_maximum_filename_length(%{original_filename: filename} = assigns) do
if String.length(filename) < 256,
do: assigns,
else: raise("filename length is more than maximum 255 characters")
end
defp ensure_limits_for_subscription(
%{
tmp_path: tmp_path,
subscription: %{
id: subscription_id,
storage_limit_kilobytes: storage_limit_kilobytes,
deer_files_limit: deer_files_limit
}
} = assigns
) do
%File.Stat{size: filesize_bytes} = File.stat!(tmp_path)
filesize_kilobytes = ceil(filesize_bytes / 1024)
{files_count, used_storage_kilobytes} =
DeerCache.SubscriptionStorageCache.fetch_data(subscription_id)
if storage_limit_kilobytes - used_storage_kilobytes > filesize_kilobytes &&
files_count < deer_files_limit do
Map.merge(assigns, %{kilobytes: filesize_kilobytes})
else
{name, _arity} = __ENV__.function
{:error, name}
end
end
defp copy_file!({:error, _} = result), do: result
defp copy_file!(
%{
tmp_path: tmp_path,
id: id,
subscription_id: subscription_id,
record: %{id: record_id, deer_table_id: table_id}
} = assigns
) do
dir_path = File.cwd!() <> "/uploaded_files/#{subscription_id}/#{table_id}/#{record_id}"
File.mkdir_p!(dir_path)
File.cp!(tmp_path, dir_path <> "/#{id}")
assigns
end
defp detect_and_assign_mime_type({:error, _} = result), do: result
defp detect_and_assign_mime_type(%{tmp_path: tmp_path} = assigns) do
{mimetype_with_newline, 0 = _exit_status} =
System.cmd("file", ["--mime-type", "-b", tmp_path])
Map.put(assigns, :mimetype, String.trim(mimetype_with_newline))
end
defp notify_subscription_storage_cache({:error, _} = result), do: result
defp notify_subscription_storage_cache(
%{subscription: %{id: subscription_id}, kilobytes: kilobytes} = assigns
) do
GenServer.cast(
DeerCache.SubscriptionStorageCache,
{:uploaded_file, subscription_id, kilobytes}
)
assigns
end
defp update_record({:error, _} = result), do: result
defp update_record(%{record: record} = assigns) do
{:ok, _record} = prepend_record_with_deer_file!(record, Map.from_struct(assigns))
end
end