# frozen_string_literal: true module PostsHelper def image_container_data(post, thumbnail=nil) display_hidden = post.hidden_from_users? && post.can_see_when_hidden?(current_user) thumbnail ||= post { 'image-id' => post.id, 'image-tags' => post.tag_ids.to_json, 'image-tag-aliases' => post.tag_list_plus_alias, 'score' => post.score, 'faves' => post.faves_count, 'upvotes' => post.upvotes_count, 'downvotes' => post.downvotes_count, 'comment-count' => post.comments_count, 'created-at' => post.created_at.iso8601, 'source-url' => post.source_url, 'uris' => thumbnail.media.file.view_urls(hidden: display_hidden).to_json, 'width' => thumbnail.image&.width, 'height' => thumbnail.image&.height, 'aspect-ratio' => thumbnail.image&.aspect_ratio # 'download-uri' => image.image.pretty_url(download: true), # 'sha512' => image.image_sha512_hash, # 'orig-sha512' => image.image_orig_sha512_hash } end # Returns a link to the image preview (:medium) or, if the image is spoilered, to the tag image. def spoilered_preview(image, filter) spoilered_tag = filter.image_spoiler_tag(image) # TODO: Thumbs for pastes? if image.media_type == 'image' && image.image.thumbnails_generated? if spoilered_tag spoilered_tag.spoiler_image_uri || asset_url('/tagblocked.svg') else see_hidden = image.hidden_from_users && image.can_see_when_hidden?(current_user) image.url(:medium, hidden: see_hidden) end else path_to_image('loading/800x600.jpg') end end def hidden_image_link(html) href = /href="([^"]*)"/.match(html)[1] rescue nil src = /src="([^"]*)"/.match(html)[1] rescue '' alt = /alt="([^"]*)"/.match(html)[1] rescue '' # All of these have been html-escaped already. linktxt = href ? " (linked to here)" : '' "Image hidden - click to view#{linktxt}" end # Toggle link for deleted images in image list pages. def toggle_deleted_images if can?(:undelete, Post) deletion_switch_arg = params[:del] markups = [] if deletion_switch_arg.present? markups.push link_to(' Hide Deleted'.html_safe, params.permit!.merge(del: nil), title: 'Hide Deleted/Merged Posts') else markups.push link_to(' Deleted'.html_safe, params.permit!.merge(del: 1), title: 'Include Deleted/Merged Posts') end if deletion_switch_arg == 'only' markups.push link_to(' Show Non-Deleted'.html_safe, params.permit!.merge(del: 1), title: 'Include Posts Visible to Users') else markups.push link_to(' Only Deleted'.html_safe, params.permit!.merge(del: 'only'), title: 'Only Deleted/Merged Posts') end markups.join(' ').html_safe end end def deleted_images_dropdown(additional_class = nil) selected = if @include_deleted.blank? '' elsif @include_deleted == :only 'only' else '1' end options = options_for_select([ [t('posts.search.exclude_deleted'), '', { title: t('posts.search.exclude_deleted_title') }], [t('posts.search.include_deleted'), '1', { title: t('posts.search.include_deleted_title') }], [t('posts.search.deleted_only'), 'only', { title: t('posts.search.deleted_only_title') }] ], selected) select_tag :del, options, autocomplete: 'off', class: "input #{additional_class}" end def get_image_or_merge_target(id) image = Post.find_by(id: id) if image&.duplicate_id Post.find_by(id: image.duplicate_id) else image end end def kb_or_mb(bytes) return '(unknown) B' if bytes.nil? return "#{bytes} B" if bytes <= 1024 size_kb = bytes / 1024 size_mb = (size_kb / 1024.0).round(2) size_kb <= 1024 ? "#{size_kb} kB " : "#{size_mb} MB " end def img_srcset(image) image.file.thumb_view_urls.map do |url, width| "#{url} #{width}w" end.join ', ' end def dense_thumbnails? current_user ? !current_user.show_large_thumbnails : false end end