2022-05-12 23:05:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-12 18:05:41 +08:00
|
|
|
class AssetSerializer < ActiveModel::Serializer
|
2022-06-03 17:52:10 +08:00
|
|
|
include Canaid::Helpers::PermissionsHelper
|
2022-05-12 18:05:41 +08:00
|
|
|
include Rails.application.routes.url_helpers
|
2022-05-12 23:05:07 +08:00
|
|
|
include FileIconsHelper
|
|
|
|
include ActionView::Helpers::NumberHelper
|
2023-03-15 23:01:01 +08:00
|
|
|
include InputSanitizeHelper
|
2022-05-13 19:07:50 +08:00
|
|
|
include ApplicationHelper
|
2022-05-12 23:05:07 +08:00
|
|
|
|
2022-05-16 17:05:18 +08:00
|
|
|
attributes :file_name, :view_mode, :icon, :urls, :updated_at_formatted,
|
|
|
|
:file_size, :medium_preview, :large_preview, :asset_type, :wopi,
|
2022-05-17 16:39:17 +08:00
|
|
|
:wopi_context, :pdf_previewable, :file_size_formatted, :asset_order,
|
2022-09-02 21:09:56 +08:00
|
|
|
:updated_at, :metadata, :image_editable, :image_context, :pdf, :attached
|
2022-05-12 23:05:07 +08:00
|
|
|
|
|
|
|
def icon
|
|
|
|
file_fa_icon_class(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_name
|
2023-03-15 23:01:01 +08:00
|
|
|
escape_input(object.render_file_name)
|
2022-05-12 23:05:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at
|
2022-05-16 17:05:18 +08:00
|
|
|
object.updated_at.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at_formatted
|
2022-05-12 23:05:07 +08:00
|
|
|
I18n.l(object.updated_at, format: :full_date) if object.updated_at
|
|
|
|
end
|
|
|
|
|
2022-09-02 21:09:56 +08:00
|
|
|
def attached
|
|
|
|
object.file.attached?
|
|
|
|
end
|
|
|
|
|
2022-05-13 19:07:50 +08:00
|
|
|
def file_size_formatted
|
2022-05-12 23:05:07 +08:00
|
|
|
number_to_human_size(object.file_size)
|
|
|
|
end
|
2022-05-12 18:05:41 +08:00
|
|
|
|
2022-05-12 23:05:07 +08:00
|
|
|
def medium_preview
|
2022-05-13 19:07:50 +08:00
|
|
|
rails_representation_url(object.medium_preview) if object.previewable?
|
|
|
|
end
|
|
|
|
|
|
|
|
def large_preview
|
|
|
|
rails_representation_url(object.large_preview) if object.previewable?
|
|
|
|
end
|
|
|
|
|
|
|
|
def asset_type
|
|
|
|
object.file.metadata&.dig(:asset_type)
|
|
|
|
end
|
|
|
|
|
2022-05-17 16:39:17 +08:00
|
|
|
def metadata
|
|
|
|
object.file.metadata
|
|
|
|
end
|
|
|
|
|
2022-05-13 19:07:50 +08:00
|
|
|
def wopi
|
|
|
|
wopi_enabled? && wopi_file?(object)
|
|
|
|
end
|
|
|
|
|
2022-05-17 16:39:17 +08:00
|
|
|
def wopi_context
|
|
|
|
if wopi
|
|
|
|
edit_supported, title = wopi_file_edit_button_status(object)
|
|
|
|
{
|
|
|
|
edit_supported: edit_supported,
|
|
|
|
title: title,
|
|
|
|
button_text: wopi_button_text(object, 'edit'),
|
2023-07-25 15:52:31 +08:00
|
|
|
wopi_icon: ActionController::Base.helpers.image_path(file_application_url(object)),
|
|
|
|
sn_icon: sn_icon_for(object)
|
2022-05-17 16:39:17 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-13 19:07:50 +08:00
|
|
|
def pdf_previewable
|
|
|
|
object.pdf_previewable? if object.file.attached?
|
2022-05-12 23:05:07 +08:00
|
|
|
end
|
2022-05-12 18:05:41 +08:00
|
|
|
|
2022-08-09 16:13:43 +08:00
|
|
|
def pdf
|
2022-08-10 16:58:39 +08:00
|
|
|
return unless object.pdf?
|
2022-08-09 16:13:43 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
url: object.pdf? ? asset_download_path(object) : asset_pdf_preview_path(object),
|
|
|
|
size: !object.pdf? && object.pdf_preview_ready? ? object.file_pdf_preview&.blob&.byte_size : object.file_size,
|
2023-05-16 16:24:08 +08:00
|
|
|
worker_url: ActionController::Base.helpers.asset_path('pdf_js_worker.js')
|
2022-08-09 16:13:43 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-05-17 16:39:17 +08:00
|
|
|
def image_editable
|
|
|
|
object.editable_image?
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_context
|
|
|
|
if image_editable
|
|
|
|
{
|
|
|
|
quality: object.file_image_quality || 80,
|
|
|
|
type: object.file.content_type
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-16 17:05:18 +08:00
|
|
|
def asset_order
|
|
|
|
case object.view_mode
|
|
|
|
when 'inline'
|
|
|
|
0
|
|
|
|
when 'thumbnail'
|
|
|
|
1
|
|
|
|
when 'list'
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-12 23:05:07 +08:00
|
|
|
def urls
|
2022-05-13 19:07:50 +08:00
|
|
|
urls = {
|
|
|
|
preview: asset_file_preview_path(object),
|
2022-09-02 21:09:56 +08:00
|
|
|
download: (rails_blob_path(object.file, disposition: 'attachment') if attached),
|
2022-05-17 16:39:17 +08:00
|
|
|
load_asset: load_asset_path(object),
|
|
|
|
asset_file: asset_file_url_path(object),
|
2022-06-03 17:52:10 +08:00
|
|
|
marvin_js: marvin_js_asset_path(object),
|
2022-06-03 21:26:16 +08:00
|
|
|
marvin_js_icon: image_path('icon_small/marvinjs.svg')
|
2022-06-03 17:52:10 +08:00
|
|
|
}
|
2022-08-09 23:43:27 +08:00
|
|
|
user = scope[:user] || @instance_options[:user]
|
|
|
|
if can_manage_asset?(user, object)
|
2022-06-03 21:26:16 +08:00
|
|
|
urls.merge!(
|
|
|
|
toggle_view_mode: toggle_view_mode_path(object),
|
|
|
|
edit_asset: edit_asset_path(object),
|
|
|
|
marvin_js_start_edit: start_editing_marvin_js_asset_path(object),
|
|
|
|
start_edit_image: start_edit_image_path(object),
|
|
|
|
delete: asset_destroy_path(object)
|
|
|
|
)
|
|
|
|
end
|
2022-08-09 23:43:27 +08:00
|
|
|
urls[:wopi_action] = object.get_action_url(user, 'embedview') if wopi && can_manage_asset?(user, object)
|
2022-05-13 19:07:50 +08:00
|
|
|
urls[:blob] = rails_blob_path(object.file, disposition: 'attachment') if object.file.attached?
|
|
|
|
|
|
|
|
urls
|
2022-05-12 23:05:07 +08:00
|
|
|
end
|
2022-05-12 18:05:41 +08:00
|
|
|
end
|