From be10ad46630138697fa55ad85a8370796cb99731 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 6 Jul 2020 14:00:02 -0400 Subject: [PATCH] add computed duration field --- lib/philomena/elasticsearch.ex | 2 +- lib/philomena/images/elasticsearch_index.ex | 6 ++++++ lib/philomena/images/image.ex | 3 +++ lib/philomena/images/query.ex | 6 +++--- lib/philomena/uploader.ex | 1 + lib/philomena_web/image_sorter.ex | 3 +++ .../templates/image/_image_meta.html.slime | 8 ++++++++ lib/philomena_web/templates/image/new.html.slime | 1 + lib/philomena_web/views/api/json/image_view.ex | 2 ++ .../20200706171350_add_duration_to_images.exs | 14 ++++++++++++++ priv/repo/structure.sql | 4 +++- 11 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 priv/repo/migrations/20200706171350_add_duration_to_images.exs diff --git a/lib/philomena/elasticsearch.ex b/lib/philomena/elasticsearch.ex index 4edd0c3b..9374ce47 100644 --- a/lib/philomena/elasticsearch.ex +++ b/lib/philomena/elasticsearch.ex @@ -52,7 +52,7 @@ defmodule Philomena.Elasticsearch do index_name = index.index_name() mapping = index.mapping().mappings.properties - Elastix.Mapping.put(elastic_url(), index_name, "_doc", %{properties: mapping}) + Elastix.Mapping.put(elastic_url(), index_name, "_doc", %{properties: mapping}, include_type_name: true) end def index_document(doc, module) do diff --git a/lib/philomena/images/elasticsearch_index.ex b/lib/philomena/images/elasticsearch_index.ex index 3e94e1a0..193bab3d 100644 --- a/lib/philomena/images/elasticsearch_index.ex +++ b/lib/philomena/images/elasticsearch_index.ex @@ -31,6 +31,7 @@ defmodule Philomena.Images.ElasticsearchIndex do downvoters: %{type: "keyword"}, downvotes: %{type: "integer"}, duplicate_id: %{type: "integer"}, + duration: %{type: "float"}, faves: %{type: "integer"}, favourited_by_user_ids: %{type: "keyword"}, favourited_by_users: %{type: "keyword"}, @@ -46,7 +47,9 @@ defmodule Philomena.Images.ElasticsearchIndex do mime_type: %{type: "keyword"}, orig_sha512_hash: %{type: "keyword"}, original_format: %{type: "keyword"}, + pixels: %{type: "integer"}, score: %{type: "integer"}, + size: %{type: "integer"}, sha512_hash: %{type: "keyword"}, source_url: %{type: "keyword"}, tag_count: %{type: "integer"}, @@ -93,6 +96,9 @@ defmodule Philomena.Images.ElasticsearchIndex do comment_count: image.comments_count, width: image.image_width, height: image.image_height, + pixels: image.image_width * image.image_height, + size: image.image_size, + duration: image.image_duration, tag_count: length(image.tags), aspect_ratio: image.image_aspect_ratio, wilson_score: wilson_score(image), diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index 3868ae24..ab7f25b2 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -49,6 +49,7 @@ defmodule Philomena.Images.Image do field :image_format, :string field :image_mime_type, :string field :image_aspect_ratio, :float + field :image_duration, :float field :image_is_animated, :boolean, source: :is_animated field :ip, EctoNetwork.INET field :fingerprint, :string @@ -131,6 +132,7 @@ defmodule Philomena.Images.Image do :image_format, :image_mime_type, :image_aspect_ratio, + :image_duration, :image_orig_sha512_hash, :image_sha512_hash, :uploaded_image, @@ -145,6 +147,7 @@ defmodule Philomena.Images.Image do :image_format, :image_mime_type, :image_aspect_ratio, + :image_duration, :image_orig_sha512_hash, :image_sha512_hash, :uploaded_image, diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index faec17cc..d7af3b0f 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -69,11 +69,11 @@ defmodule Philomena.Images.Query do defp anonymous_fields do [ int_fields: - ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count), - float_fields: ~W(aspect_ratio wilson_score), + ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count pixels size), + float_fields: ~W(aspect_ratio wilson_score duration), date_fields: ~W(created_at updated_at first_seen_at), literal_fields: - ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format), + ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format mime_type), ngram_fields: ~W(description), custom_fields: ~W(gallery_id), default_field: {"namespaced_tags.name", :term}, diff --git a/lib/philomena/uploader.ex b/lib/philomena/uploader.ex index c246f102..ba2cb0f4 100644 --- a/lib/philomena/uploader.ex +++ b/lib/philomena/uploader.ex @@ -31,6 +31,7 @@ defmodule Philomena.Uploader do "size" => analysis.size, "format" => analysis.extension, "mime_type" => analysis.mime_type, + "duration" => analysis.duration, "aspect_ratio" => analysis.aspect_ratio, "orig_sha512_hash" => analysis.sha512, "sha512_hash" => analysis.sha512, diff --git a/lib/philomena_web/image_sorter.ex b/lib/philomena_web/image_sorter.ex index a58a9def..f727dd72 100644 --- a/lib/philomena_web/image_sorter.ex +++ b/lib/philomena_web/image_sorter.ex @@ -14,6 +14,9 @@ defmodule PhilomenaWeb.ImageSorter do comment_count tag_count wilson_score + pixels + size + duration ) def parse_sort(params, query) do diff --git a/lib/philomena_web/templates/image/_image_meta.html.slime b/lib/philomena_web/templates/image/_image_meta.html.slime index 503f1e03..ef7f9ef3 100644 --- a/lib/philomena_web/templates/image/_image_meta.html.slime +++ b/lib/philomena_web/templates/image/_image_meta.html.slime @@ -61,6 +61,14 @@ = @image.image_width | x = @image.image_height + + = if not is_nil(@image.image_duration) and @image.image_is_animated and @image.image_duration > 0 do + span.image-size + |   + - dur = ceil(@image.image_duration) + - {hh, mm, ss} = {div(dur, 3600), div(rem(dur, 3600), 60), rem(dur, 60)} + = :io_lib.format("~2..0B:~2..0B:~2..0B", [hh, mm, ss]) + =<> String.upcase(to_string(@image.image_format)) - size_kb = div(@image.image_size, 1000) - size_mb = Float.round(size_kb / 1000.0, 2) diff --git a/lib/philomena_web/templates/image/new.html.slime b/lib/philomena_web/templates/image/new.html.slime index 10cae856..2e9b92ae 100644 --- a/lib/philomena_web/templates/image/new.html.slime +++ b/lib/philomena_web/templates/image/new.html.slime @@ -36,6 +36,7 @@ = error_tag f, :image_height = error_tag f, :image_name = error_tag f, :image_mime_type + = error_tag f, :image_duration = error_tag f, :image_orig_sha512_hash .field.field--inline diff --git a/lib/philomena_web/views/api/json/image_view.ex b/lib/philomena_web/views/api/json/image_view.ex index d0985a85..ec0a9699 100644 --- a/lib/philomena_web/views/api/json/image_view.ex +++ b/lib/philomena_web/views/api/json/image_view.ex @@ -59,6 +59,8 @@ defmodule PhilomenaWeb.Api.Json.ImageView do width: image.image_width, height: image.image_height, mime_type: image.image_mime_type, + size: image.image_size, + duration: image.image_duration, format: image.image_format, aspect_ratio: image.image_aspect_ratio, name: image.image_name, diff --git a/priv/repo/migrations/20200706171350_add_duration_to_images.exs b/priv/repo/migrations/20200706171350_add_duration_to_images.exs new file mode 100644 index 00000000..d9cd28f7 --- /dev/null +++ b/priv/repo/migrations/20200706171350_add_duration_to_images.exs @@ -0,0 +1,14 @@ +defmodule Philomena.Repo.Migrations.AddDurationToImages do + use Ecto.Migration + + def change do + alter table("images") do + add :image_duration, :float + end + + # After successful migration: + # alias Philomena.Elasticsearch + # alias Philomena.Images.Image + # Elasticsearch.update_mapping!(Image) + end +end diff --git a/priv/repo/structure.sql b/priv/repo/structure.sql index 7193f6a2..04ce14c9 100644 --- a/priv/repo/structure.sql +++ b/priv/repo/structure.sql @@ -882,7 +882,8 @@ CREATE TABLE public.images ( destroyed_content boolean DEFAULT false NOT NULL, hidden_image_key character varying, scratchpad character varying, - hides_count integer DEFAULT 0 NOT NULL + hides_count integer DEFAULT 0 NOT NULL, + image_duration double precision ); @@ -4706,3 +4707,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20200503002523); INSERT INTO public."schema_migrations" (version) VALUES (20200607000511); INSERT INTO public."schema_migrations" (version) VALUES (20200617111116); INSERT INTO public."schema_migrations" (version) VALUES (20200617113333); +INSERT INTO public."schema_migrations" (version) VALUES (20200706171350);