2019-07-10 20:37:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Asset < ApplicationRecord
|
2016-02-12 23:52:43 +08:00
|
|
|
include SearchableModel
|
|
|
|
include DatabaseHelper
|
2016-07-21 19:11:15 +08:00
|
|
|
include Encryptor
|
2016-08-03 21:31:25 +08:00
|
|
|
include WopiUtil
|
2019-07-10 20:37:23 +08:00
|
|
|
include ActiveStorageFileUtil
|
2019-09-23 22:33:57 +08:00
|
|
|
include ActiveStorageConcerns
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
require 'tempfile'
|
2016-08-10 23:49:25 +08:00
|
|
|
# Lock duration set to 30 minutes
|
2019-06-24 21:17:42 +08:00
|
|
|
LOCK_DURATION = 60 * 30
|
|
|
|
|
2020-11-17 22:33:25 +08:00
|
|
|
enum view_mode: { thumbnail: 0, list: 1, inline: 2 }
|
2020-09-02 21:02:56 +08:00
|
|
|
|
2019-06-24 21:17:42 +08:00
|
|
|
# ActiveStorage configuration
|
|
|
|
has_one_attached :file
|
2020-12-16 19:13:42 +08:00
|
|
|
has_one_attached :file_pdf_preview
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Asset validation
|
|
|
|
# This could cause some problems if you create empty asset and want to
|
|
|
|
# assign it to result
|
2018-03-12 17:50:45 +08:00
|
|
|
validate :step_or_result_or_repository_asset_value
|
2019-10-01 18:50:54 +08:00
|
|
|
validate :wopi_filename_valid, on: :wopi_file_creation
|
|
|
|
validate :check_file_size, on: :on_api_upload
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: 'created_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2016-12-08 22:24:14 +08:00
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: 'last_modified_by_id',
|
2017-06-28 21:21:32 +08:00
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :team, optional: true
|
2018-03-10 00:04:54 +08:00
|
|
|
has_one :step_asset, inverse_of: :asset, dependent: :destroy
|
2020-10-06 19:53:53 +08:00
|
|
|
has_one :step, through: :step_asset, touch: true, dependent: :nullify
|
2018-03-10 00:04:54 +08:00
|
|
|
has_one :result_asset, inverse_of: :asset, dependent: :destroy
|
2020-10-06 19:53:53 +08:00
|
|
|
has_one :result, through: :result_asset, touch: true, dependent: :nullify
|
2018-03-10 00:04:54 +08:00
|
|
|
has_one :repository_asset_value, inverse_of: :asset, dependent: :destroy
|
|
|
|
has_one :repository_cell, through: :repository_asset_value,
|
2016-02-12 23:52:43 +08:00
|
|
|
dependent: :nullify
|
|
|
|
has_many :report_elements, inverse_of: :asset, dependent: :destroy
|
|
|
|
has_one :asset_text_datum, inverse_of: :asset, dependent: :destroy
|
|
|
|
|
2020-11-17 22:33:25 +08:00
|
|
|
scope :sort_assets, lambda { |sort_value = 'new'|
|
|
|
|
sort = case sort_value
|
|
|
|
when 'old' then { created_at: :asc }
|
|
|
|
when 'atoz' then { 'active_storage_blobs.filename': :asc }
|
|
|
|
when 'ztoa' then { 'active_storage_blobs.filename': :desc }
|
|
|
|
else { created_at: :desc }
|
|
|
|
end
|
|
|
|
|
|
|
|
joins(file_attachment: :blob).order(sort)
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
attr_accessor :file_content, :file_info, :in_template
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-07-07 20:57:35 +08:00
|
|
|
before_save :reset_file_processing, if: -> { file.new_record? }
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def self.search(
|
|
|
|
user,
|
2018-07-26 15:55:18 +08:00
|
|
|
include_archived,
|
2016-02-12 23:52:43 +08:00
|
|
|
query = nil,
|
2017-05-05 22:41:23 +08:00
|
|
|
page = 1,
|
|
|
|
_current_team = nil,
|
|
|
|
options = {}
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
|
|
|
|
2018-09-17 21:46:56 +08:00
|
|
|
teams = user.teams.select(:id)
|
|
|
|
|
|
|
|
assets_in_steps = Asset.joins(:step).where(
|
|
|
|
'steps.id IN (?)',
|
|
|
|
Step.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
|
|
|
.select(:id)
|
|
|
|
).pluck(:id)
|
|
|
|
|
|
|
|
assets_in_results = Asset.joins(:result).where(
|
|
|
|
'results.id IN (?)',
|
|
|
|
Result.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
|
|
|
.select(:id)
|
|
|
|
).pluck(:id)
|
|
|
|
|
|
|
|
assets_in_inventories = Asset.joins(
|
|
|
|
repository_cell: { repository_column: :repository }
|
|
|
|
).where('repositories.team_id IN (?)', teams).pluck(:id)
|
|
|
|
|
|
|
|
assets =
|
|
|
|
Asset.distinct
|
|
|
|
.where('assets.id IN (?) OR assets.id IN (?) OR assets.id IN (?)',
|
|
|
|
assets_in_steps, assets_in_results, assets_in_inventories)
|
|
|
|
|
|
|
|
new_query = Asset.left_outer_joins(:asset_text_datum)
|
2019-08-02 21:17:28 +08:00
|
|
|
.joins(file_attachment: :blob)
|
2018-09-17 21:46:56 +08:00
|
|
|
.from(assets, 'assets')
|
2017-05-05 22:41:23 +08:00
|
|
|
|
|
|
|
a_query = s_query = ''
|
|
|
|
|
|
|
|
if options[:whole_word].to_s == 'true' ||
|
|
|
|
options[:whole_phrase].to_s == 'true'
|
|
|
|
like = options[:match_case].to_s == 'true' ? '~' : '~*'
|
|
|
|
s_query = query.gsub(/[!()&|:]/, ' ')
|
|
|
|
.strip
|
|
|
|
.split(/\s+/)
|
|
|
|
.map { |t| t + ':*' }
|
|
|
|
if options[:whole_word].to_s == 'true'
|
|
|
|
a_query = query.split
|
|
|
|
.map { |a| Regexp.escape(a) }
|
|
|
|
.join('|')
|
|
|
|
s_query = s_query.join('|')
|
|
|
|
else
|
|
|
|
a_query = Regexp.escape(query)
|
|
|
|
s_query = s_query.join('&')
|
|
|
|
end
|
|
|
|
a_query = '\\y(' + a_query + ')\\y'
|
|
|
|
s_query = s_query.tr('\'', '"')
|
|
|
|
|
2018-03-26 19:19:09 +08:00
|
|
|
new_query = new_query.where(
|
2019-07-09 16:28:15 +08:00
|
|
|
"(active_storage_blobs.filename #{like} ? " \
|
2016-07-21 19:11:15 +08:00
|
|
|
"OR asset_text_data.data_vector @@ to_tsquery(?))",
|
|
|
|
a_query,
|
|
|
|
s_query
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
2017-05-05 22:41:23 +08:00
|
|
|
else
|
|
|
|
like = options[:match_case].to_s == 'true' ? 'LIKE' : 'ILIKE'
|
|
|
|
a_query = query.split.map { |a| "%#{sanitize_sql_like(a)}%" }
|
|
|
|
|
|
|
|
# Trim whitespace and replace it with OR character. Make prefixed
|
|
|
|
# wildcard search term and escape special characters.
|
|
|
|
# For example, search term 'demo project' is transformed to
|
|
|
|
# 'demo:*|project:*' which makes word inclusive search with postfix
|
|
|
|
# wildcard.
|
|
|
|
s_query = query.gsub(/[!()&|:]/, ' ')
|
|
|
|
.strip
|
|
|
|
.split(/\s+/)
|
|
|
|
.map { |t| t + ':*' }
|
|
|
|
.join('|')
|
|
|
|
.tr('\'', '"')
|
2018-03-26 19:19:09 +08:00
|
|
|
new_query = new_query.where(
|
2019-07-09 16:28:15 +08:00
|
|
|
"(active_storage_blobs.filename #{like} ANY (array[?]) " \
|
2017-05-05 22:41:23 +08:00
|
|
|
"OR asset_text_data.data_vector @@ to_tsquery(?))",
|
|
|
|
a_query,
|
|
|
|
s_query
|
|
|
|
)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Show all results if needed
|
2016-10-05 23:45:20 +08:00
|
|
|
if page != Constants::SEARCH_NO_LIMIT
|
2018-07-26 15:55:18 +08:00
|
|
|
new_query = new_query.select('assets.*, asset_text_data.data AS data')
|
|
|
|
.limit(Constants::SEARCH_LIMIT)
|
|
|
|
.offset((page - 1) * Constants::SEARCH_LIMIT)
|
|
|
|
Asset.select(
|
|
|
|
"assets_search.*, ts_headline(assets_search.data, to_tsquery('" +
|
|
|
|
sanitize_sql_for_conditions(s_query) +
|
|
|
|
"'), 'StartSel=<mark>, StopSel=</mark>') AS headline"
|
|
|
|
).from(new_query, 'assets_search')
|
2018-03-26 19:19:09 +08:00
|
|
|
else
|
|
|
|
new_query
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def blob
|
|
|
|
file&.blob
|
|
|
|
end
|
|
|
|
|
2019-06-28 14:17:09 +08:00
|
|
|
def previewable?
|
2019-07-02 05:30:20 +08:00
|
|
|
return false unless file.attached?
|
|
|
|
|
2019-07-10 20:37:23 +08:00
|
|
|
previewable_document?(blob) || previewable_image?
|
2019-06-28 14:17:09 +08:00
|
|
|
end
|
|
|
|
|
2019-06-24 21:17:42 +08:00
|
|
|
def medium_preview
|
2019-09-24 03:07:03 +08:00
|
|
|
file.representation(resize_to_limit: Constants::MEDIUM_PIC_FORMAT)
|
2019-06-24 21:17:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def large_preview
|
2019-09-24 03:07:03 +08:00
|
|
|
file.representation(resize_to_limit: Constants::LARGE_PIC_FORMAT)
|
2019-06-28 14:17:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_name
|
2019-07-02 05:30:20 +08:00
|
|
|
return '' unless file.attached?
|
|
|
|
|
2019-07-10 20:37:23 +08:00
|
|
|
file.blob&.filename&.sanitized
|
2019-06-24 21:17:42 +08:00
|
|
|
end
|
|
|
|
|
2020-11-25 19:50:34 +08:00
|
|
|
def render_file_name
|
|
|
|
if file.attached? && file.metadata['asset_type']
|
|
|
|
file.metadata['name']
|
|
|
|
else
|
|
|
|
file_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-24 21:17:42 +08:00
|
|
|
def file_size
|
2019-07-02 05:30:20 +08:00
|
|
|
return 0 unless file.attached?
|
|
|
|
|
2019-06-28 14:17:09 +08:00
|
|
|
file.blob&.byte_size
|
2019-06-24 21:17:42 +08:00
|
|
|
end
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def content_type
|
2019-07-10 20:37:23 +08:00
|
|
|
return '' unless file.attached?
|
2019-07-09 16:28:15 +08:00
|
|
|
|
2019-07-10 20:37:23 +08:00
|
|
|
file&.blob&.content_type
|
2019-07-09 16:28:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate
|
|
|
|
new_asset = dup
|
|
|
|
return unless new_asset.save
|
|
|
|
|
|
|
|
duplicate_file(new_asset)
|
|
|
|
new_asset
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate_file(to_asset)
|
2019-09-30 16:25:11 +08:00
|
|
|
return unless file.attached?
|
|
|
|
|
|
|
|
raise ArgumentError, 'Destination asset should be persisted first!' unless to_asset.persisted?
|
|
|
|
|
|
|
|
file.blob.open do |tmp_file|
|
2021-01-28 21:43:45 +08:00
|
|
|
to_blob = ActiveStorage::Blob.create_and_upload!(io: tmp_file, filename: blob.filename, metadata: blob.metadata)
|
2019-09-30 16:25:11 +08:00
|
|
|
to_asset.file.attach(to_blob)
|
|
|
|
end
|
2019-08-05 17:30:36 +08:00
|
|
|
to_asset.post_process_file(to_asset.team)
|
2019-07-09 16:28:15 +08:00
|
|
|
end
|
|
|
|
|
2019-06-28 14:17:09 +08:00
|
|
|
def image?
|
2019-07-12 21:47:15 +08:00
|
|
|
content_type =~ %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2016-09-06 16:03:27 +08:00
|
|
|
def text?
|
2016-10-05 23:45:20 +08:00
|
|
|
Constants::TEXT_EXTRACT_FILE_TYPES.any? do |v|
|
2019-07-09 16:28:15 +08:00
|
|
|
file&.blob&.content_type&.start_with? v
|
2016-10-05 23:45:20 +08:00
|
|
|
end
|
2016-09-06 16:03:27 +08:00
|
|
|
end
|
|
|
|
|
2019-08-02 21:17:28 +08:00
|
|
|
def marvinjs?
|
|
|
|
file.metadata[:asset_type] == 'marvinjs'
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2021-07-20 19:06:47 +08:00
|
|
|
def bio_eddie?
|
|
|
|
file.metadata[:asset_type] == 'bio_eddie' || File.extname(file_name) == '.helm'
|
|
|
|
end
|
|
|
|
|
2020-12-16 19:13:42 +08:00
|
|
|
def pdf_preview_ready?
|
|
|
|
return false if pdf_preview_processing
|
|
|
|
|
|
|
|
return true if file_pdf_preview.attached?
|
|
|
|
|
|
|
|
PdfPreviewJob.perform_later(id)
|
|
|
|
ActiveRecord::Base.no_touching { update(pdf_preview_processing: true) }
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-12-22 22:25:30 +08:00
|
|
|
def pdf?
|
|
|
|
content_type == 'application/pdf'
|
|
|
|
end
|
|
|
|
|
|
|
|
def pdf_previewable?
|
|
|
|
pdf? || (previewable_document?(blob) && Rails.application.config.x.enable_pdf_previews)
|
|
|
|
end
|
|
|
|
|
2017-01-24 23:34:21 +08:00
|
|
|
def post_process_file(team = nil)
|
2016-02-12 23:52:43 +08:00
|
|
|
# Extract asset text if it's of correct type
|
2016-09-06 16:03:27 +08:00
|
|
|
if text?
|
2016-02-12 23:52:43 +08:00
|
|
|
Rails.logger.info "Asset #{id}: Creating extract text job"
|
|
|
|
# The extract_asset_text also includes
|
|
|
|
# estimated size calculation
|
2020-09-29 19:11:50 +08:00
|
|
|
Asset.delay(queue: :assets).extract_asset_text_delayed(id, in_template)
|
2019-08-02 21:17:28 +08:00
|
|
|
elsif marvinjs?
|
|
|
|
extract_asset_text
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
|
|
|
# Update asset's estimated size immediately
|
2017-01-24 23:34:21 +08:00
|
|
|
update_estimated_size(team)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2020-12-16 19:13:42 +08:00
|
|
|
|
|
|
|
if Rails.application.config.x.enable_pdf_previews && previewable_document?(blob)
|
|
|
|
PdfPreviewJob.perform_later(id)
|
|
|
|
ActiveRecord::Base.no_touching { update(pdf_preview_processing: true) }
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def self.extract_asset_text_delayed(asset_id, in_template = false)
|
2019-09-23 22:33:57 +08:00
|
|
|
asset = find_by(id: asset_id)
|
2019-07-09 16:28:15 +08:00
|
|
|
return unless asset.present? && asset.file.attached?
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
asset.extract_asset_text(in_template)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def extract_asset_text(in_template = false)
|
|
|
|
self.in_template = in_template
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-08-02 21:17:28 +08:00
|
|
|
if marvinjs?
|
|
|
|
mjs_doc = Nokogiri::XML(file.metadata[:description])
|
|
|
|
mjs_doc.remove_namespaces!
|
|
|
|
text_data = mjs_doc.search("//Field[@name='text']").collect(&:text).join(' ')
|
|
|
|
else
|
2019-02-07 18:06:25 +08:00
|
|
|
# Start Tika as a server
|
2022-03-30 20:08:36 +08:00
|
|
|
Yomu.server(:text)
|
|
|
|
begin
|
|
|
|
blob.open do |tmp_file|
|
|
|
|
text_data = Yomu.new(tmp_file.path).text
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Yomu.kill_server!
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2019-08-02 21:17:28 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-08-02 21:17:28 +08:00
|
|
|
if asset_text_datum.present?
|
|
|
|
# Update existing text datum if it exists
|
|
|
|
asset_text_datum.update(data: text_data)
|
|
|
|
else
|
|
|
|
# Create new text datum
|
|
|
|
AssetTextDatum.create(data: text_data, asset: self)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2019-08-02 21:17:28 +08:00
|
|
|
Rails.logger.info "Asset #{id}: Asset file successfully extracted"
|
2016-08-17 15:44:53 +08:00
|
|
|
|
2019-08-02 21:17:28 +08:00
|
|
|
# Finally, update asset's estimated size to include
|
|
|
|
# the data vector
|
|
|
|
update_estimated_size(team)
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.fatal(
|
|
|
|
"Asset #{id}: Error extracting contents from asset "\
|
|
|
|
"file #{file.blob.key}: #{e.message}"
|
|
|
|
)
|
2017-04-06 16:59:23 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 23:34:21 +08:00
|
|
|
# If team is provided, its space_taken
|
2016-02-12 23:52:43 +08:00
|
|
|
# is updated as well
|
2017-01-24 23:34:21 +08:00
|
|
|
def update_estimated_size(team = nil)
|
2019-07-09 16:28:15 +08:00
|
|
|
return if file_size.blank? || in_template
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
es = file_size
|
2019-01-25 17:50:08 +08:00
|
|
|
if asset_text_datum.present? && asset_text_datum.persisted?
|
2016-02-12 23:52:43 +08:00
|
|
|
asset_text_datum.reload
|
|
|
|
es += get_octet_length_record(asset_text_datum, :data)
|
|
|
|
es += get_octet_length_record(asset_text_datum, :data_vector)
|
|
|
|
end
|
2016-10-07 00:36:55 +08:00
|
|
|
es *= Constants::ASSET_ESTIMATED_SIZE_FACTOR
|
2016-02-12 23:52:43 +08:00
|
|
|
update(estimated_size: es)
|
|
|
|
Rails.logger.info "Asset #{id}: Estimated size successfully calculated"
|
|
|
|
|
2017-01-24 23:34:21 +08:00
|
|
|
# Finally, update team's space
|
|
|
|
if team.present?
|
|
|
|
team.take_space(es)
|
|
|
|
team.save
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-10 23:49:25 +08:00
|
|
|
def can_perform_action(action)
|
2017-01-03 23:35:25 +08:00
|
|
|
if ENV['WOPI_ENABLED'] == 'true'
|
2019-07-09 16:28:15 +08:00
|
|
|
file_ext = file_name.split('.').last
|
2017-01-03 23:35:25 +08:00
|
|
|
|
|
|
|
if file_ext == 'wopitest' &&
|
|
|
|
(!ENV['WOPI_TEST_ENABLED'] || ENV['WOPI_TEST_ENABLED'] == 'false')
|
|
|
|
return false
|
|
|
|
end
|
2019-07-09 16:28:15 +08:00
|
|
|
|
2017-01-03 23:35:25 +08:00
|
|
|
action = get_action(file_ext, action)
|
|
|
|
return false if action.nil?
|
2019-07-09 16:28:15 +08:00
|
|
|
|
2017-01-03 23:35:25 +08:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
def get_action_url(user, action, with_tokens = true)
|
2019-07-09 16:28:15 +08:00
|
|
|
file_ext = file_name.split('.').last
|
2016-09-23 17:42:12 +08:00
|
|
|
action = get_action(file_ext, action)
|
2016-08-03 21:31:25 +08:00
|
|
|
if !action.nil?
|
2020-09-29 18:58:05 +08:00
|
|
|
action_url = action[:urlsrc]
|
2019-05-18 16:22:53 +08:00
|
|
|
if ENV['WOPI_BUSINESS_USERS'] && ENV['WOPI_BUSINESS_USERS'] == 'true'
|
2017-07-04 20:27:43 +08:00
|
|
|
action_url = action_url.gsub(/<IsLicensedUser=BUSINESS_USER&>/,
|
2017-07-04 23:13:59 +08:00
|
|
|
'IsLicensedUser=1&')
|
2017-07-04 20:27:43 +08:00
|
|
|
action_url = action_url.gsub(/<IsLicensedUser=BUSINESS_USER>/,
|
2017-07-04 23:13:59 +08:00
|
|
|
'IsLicensedUser=1')
|
|
|
|
else
|
2017-07-04 20:27:43 +08:00
|
|
|
action_url = action_url.gsub(/<IsLicensedUser=BUSINESS_USER&>/,
|
2017-07-04 23:13:59 +08:00
|
|
|
'IsLicensedUser=0&')
|
2017-07-04 20:27:43 +08:00
|
|
|
action_url = action_url.gsub(/<IsLicensedUser=BUSINESS_USER>/,
|
2017-07-04 23:13:59 +08:00
|
|
|
'IsLicensedUser=0')
|
|
|
|
end
|
2016-09-23 17:42:12 +08:00
|
|
|
action_url = action_url.gsub(/<.*?=.*?>/, '')
|
|
|
|
|
|
|
|
rest_url = Rails.application.routes.url_helpers.wopi_rest_endpoint_url(
|
|
|
|
host: ENV['WOPI_ENDPOINT_URL'],
|
|
|
|
id: id
|
|
|
|
)
|
|
|
|
action_url += "WOPISrc=#{rest_url}"
|
2016-08-10 23:49:25 +08:00
|
|
|
if with_tokens
|
2019-07-09 16:28:15 +08:00
|
|
|
token = user.get_wopi_token
|
2016-11-30 23:48:42 +08:00
|
|
|
action_url + "&access_token=#{token.token}"\
|
|
|
|
"&access_token_ttl=#{(token.ttl * 1000)}"
|
2016-08-10 23:49:25 +08:00
|
|
|
else
|
|
|
|
action_url
|
|
|
|
end
|
2016-08-03 21:31:25 +08:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-29 18:19:29 +08:00
|
|
|
def favicon_url(action)
|
2019-07-09 16:28:15 +08:00
|
|
|
file_ext = file_name.split('.').last
|
2016-09-29 18:19:29 +08:00
|
|
|
action = get_action(file_ext, action)
|
2020-09-29 18:58:05 +08:00
|
|
|
action[:icon] if action[:icon]
|
2016-09-29 18:19:29 +08:00
|
|
|
end
|
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
# locked?, lock_asset and refresh_lock rely on the asset
|
|
|
|
# being locked in the database to prevent race conditions
|
|
|
|
def locked?
|
2019-09-25 17:07:53 +08:00
|
|
|
unlock_expired
|
2016-09-23 17:42:12 +08:00
|
|
|
!lock.nil?
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def lock_asset(lock_string)
|
|
|
|
self.lock = lock_string
|
|
|
|
self.lock_ttl = Time.now.to_i + LOCK_DURATION
|
2016-09-23 17:42:12 +08:00
|
|
|
save!
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_lock
|
|
|
|
self.lock_ttl = Time.now.to_i + LOCK_DURATION
|
2016-09-23 17:42:12 +08:00
|
|
|
save!
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unlock
|
|
|
|
self.lock = nil
|
|
|
|
self.lock_ttl = nil
|
2016-09-23 17:42:12 +08:00
|
|
|
save!
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unlock_expired
|
2016-09-23 17:42:12 +08:00
|
|
|
with_lock do
|
2019-09-30 14:46:16 +08:00
|
|
|
if !lock_ttl.nil? && lock_ttl < Time.now.to_i
|
2016-08-10 23:49:25 +08:00
|
|
|
self.lock = nil
|
|
|
|
self.lock_ttl = nil
|
2016-09-23 17:42:12 +08:00
|
|
|
save!
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_contents(new_file)
|
2019-10-09 03:34:16 +08:00
|
|
|
file.attach(io: new_file, filename: file_name)
|
2016-09-23 17:42:12 +08:00
|
|
|
self.version = version.nil? ? 1 : version + 1
|
|
|
|
save
|
2016-08-10 23:49:25 +08:00
|
|
|
end
|
|
|
|
|
2019-04-02 22:38:53 +08:00
|
|
|
def editable_image?
|
2022-05-17 16:39:17 +08:00
|
|
|
!locked? && (%r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES_EDITABLE)}} =~ file.content_type).present?
|
2019-03-19 20:52:57 +08:00
|
|
|
end
|
|
|
|
|
2019-06-19 19:57:04 +08:00
|
|
|
def generate_base64(style)
|
2019-09-12 15:02:45 +08:00
|
|
|
return convert_variant_to_base64(medium_preview) if style == :medium
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2021-07-15 18:37:04 +08:00
|
|
|
def my_module
|
2021-08-03 15:58:13 +08:00
|
|
|
(result || step)&.my_module
|
2021-07-15 18:37:04 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
private
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def tempdir
|
|
|
|
Rails.root.join('tmp')
|
2016-08-05 23:00:29 +08:00
|
|
|
end
|
|
|
|
|
2019-06-28 14:17:09 +08:00
|
|
|
def previewable_image?
|
|
|
|
file.blob&.content_type =~ %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-03-12 17:50:45 +08:00
|
|
|
def step_or_result_or_repository_asset_value
|
2016-02-12 23:52:43 +08:00
|
|
|
# We must allow both step and result to be blank because of GUI
|
2016-08-17 15:44:53 +08:00
|
|
|
# (even though it's not really a "valid" asset)
|
2018-03-10 00:04:54 +08:00
|
|
|
if step.present? && result.present? ||
|
2018-03-12 17:50:45 +08:00
|
|
|
step.present? && repository_asset_value.present? ||
|
|
|
|
result.present? && repository_asset_value.present?
|
|
|
|
errors.add(
|
|
|
|
:base,
|
|
|
|
'Asset can only be result or step or repository cell, not ever.'
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-13 21:38:35 +08:00
|
|
|
def wopi_filename_valid
|
|
|
|
# Check that filename without extension is not blank
|
2019-09-23 22:33:57 +08:00
|
|
|
if file_name[0..-6].blank?
|
2019-03-21 17:00:54 +08:00
|
|
|
errors.add(
|
|
|
|
:file,
|
|
|
|
I18n.t('general.text.not_blank')
|
|
|
|
)
|
|
|
|
end
|
2019-05-13 21:38:35 +08:00
|
|
|
# Check maximum filename length
|
2019-08-21 19:26:11 +08:00
|
|
|
if file_name.length > Constants::FILENAME_MAX_LENGTH
|
2019-05-13 21:38:35 +08:00
|
|
|
errors.add(
|
|
|
|
:file,
|
|
|
|
I18n.t(
|
|
|
|
'general.file.file_name_too_long',
|
|
|
|
limit: Constants::FILENAME_MAX_LENGTH
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2019-03-21 17:00:54 +08:00
|
|
|
end
|
2019-10-01 18:50:54 +08:00
|
|
|
|
|
|
|
def check_file_size
|
|
|
|
if file.attached?
|
|
|
|
if file.blob.byte_size > Rails.application.config.x.file_max_size_mb.megabytes
|
|
|
|
errors.add(:file, I18n.t('activerecord.errors.models.asset.attributes.file.too_big'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-07-07 20:57:35 +08:00
|
|
|
|
|
|
|
def reset_file_processing
|
|
|
|
self.file_processing = false
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|