Fix ImageUploader automatic upload
* Serialize DateTime as an iso string for bot_state filesystem * Set user-agent for uploading images * Check for more types of images other than jpegpull/974/head
parent
8b2ac2935e
commit
373a0b2faf
|
@ -82,13 +82,14 @@ defmodule FarmbotCore.BotState.FileSystem do
|
|||
Enum.reduce(bot_state, acc, fn {key, value}, acc ->
|
||||
cond do
|
||||
is_map(value) && map_size(value) == 0 -> [Path.join(prefix, to_string(key)) | acc]
|
||||
is_list(value) -> raise("Arrays can not be serialized to filesystem nodes")
|
||||
match?(%DateTime{}, value) -> [{Path.join(prefix, to_string(value)), to_string(value)} | acc]
|
||||
is_map(value) -> serialize_state(value, Path.join(prefix, to_string(key)), acc)
|
||||
is_number(value) -> [{Path.join(prefix, to_string(key)), to_string(value)} | acc]
|
||||
is_binary(value) -> [{Path.join(prefix, to_string(key)), to_string(value)} | acc]
|
||||
is_atom(value) -> [{Path.join(prefix, to_string(key)), to_string(value)} | acc]
|
||||
is_boolean(value) -> [{Path.join(prefix, to_string(key)), to_string(value)} | acc]
|
||||
is_nil(value) -> [{Path.join(prefix, to_string(key)), <<0x0>>} | acc]
|
||||
is_list(value) -> raise("Arrays can not be serialized to filesystem nodes")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
|
|
@ -46,18 +46,16 @@ defmodule FarmbotExt.API do
|
|||
end
|
||||
|
||||
def storage_client(%StorageAuth{url: url}) do
|
||||
Tesla.client(
|
||||
[
|
||||
{Tesla.Middleware.BaseUrl, "https:" <> url},
|
||||
{Tesla.Middleware.Headers,
|
||||
[
|
||||
{"user-agent", "farmbot-os"}
|
||||
]}
|
||||
],
|
||||
[
|
||||
{Tesla.Middleware.FormUrlencoded, []}
|
||||
]
|
||||
)
|
||||
user_agent = "FarmbotOS/#{@version} (#{@target}) #{@target} ()"
|
||||
|
||||
Tesla.client([
|
||||
{Tesla.Middleware.BaseUrl, "https:" <> url},
|
||||
{Tesla.Middleware.Headers,
|
||||
[
|
||||
{"user-agent", user_agent}
|
||||
]},
|
||||
{Tesla.Middleware.FormUrlencoded, []}
|
||||
])
|
||||
end
|
||||
|
||||
@file_chunk 4096
|
||||
|
@ -117,7 +115,7 @@ defmodule FarmbotExt.API do
|
|||
r
|
||||
else
|
||||
er ->
|
||||
FarmbotCore.Logger.error(1, "Failed to upload image")
|
||||
FarmbotCore.Logger.error(1, "Failed to upload image: #{inspect(er)}")
|
||||
BotState.set_job_progress(image_filename, %{prog | percent: -1, status: "error"})
|
||||
er
|
||||
end
|
||||
|
|
|
@ -31,7 +31,10 @@ defmodule FarmbotExt.API.ImageUploader do
|
|||
end
|
||||
|
||||
def handle_info(:timeout, state) do
|
||||
files = Path.wildcard(Path.join(@images_path, "*.jpg"))
|
||||
files =
|
||||
Path.wildcard(Path.join(@images_path, "*"))
|
||||
|> Enum.filter(&matches_any_pattern?(&1, [~r{/tmp/images/.*(jpg|jpeg|png|gif)}]))
|
||||
|
||||
{:noreply, state, {:continue, files}}
|
||||
end
|
||||
|
||||
|
@ -44,12 +47,29 @@ defmodule FarmbotExt.API.ImageUploader do
|
|||
|
||||
# TODO(Connor) the meta here is likely inaccurate.
|
||||
defp try_upload(image_filename) do
|
||||
meta = BotState.fetch().location_data.position
|
||||
%{x: x, y: y, z: z} = BotState.fetch().location_data.position
|
||||
meta = %{x: x, y: y, z: z}
|
||||
|
||||
with {:ok, %{status: s, body: body}} when s > 199 and s < 300 <-
|
||||
with {:ok, %{status: s, body: _body}} when s > 199 and s < 300 <-
|
||||
API.upload_image(image_filename, meta) do
|
||||
FarmbotCore.Logger.success(3, "Uploaded image: #{inspect(body)}")
|
||||
FarmbotCore.Logger.success(3, "Uploaded image: #{image_filename}")
|
||||
File.rm(image_filename)
|
||||
end
|
||||
end
|
||||
|
||||
# Stolen from
|
||||
# https://github.com/phoenixframework/
|
||||
# phoenix_live_reload/blob/151ce9e17c1b4ead79062098b70d4e6bc7c7e528
|
||||
# /lib/phoenix_live_reload/channel.ex#L27
|
||||
defp matches_any_pattern?(path, patterns) do
|
||||
path = to_string(path)
|
||||
|
||||
if String.contains?(path, "~") do
|
||||
false
|
||||
else
|
||||
Enum.any?(patterns, fn pattern ->
|
||||
String.match?(path, pattern)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue