2016-08-03 21:31:25 +08:00
|
|
|
module WopiUtil
|
|
|
|
require 'open-uri'
|
|
|
|
|
2016-09-22 22:18:57 +08:00
|
|
|
# Used for timestamp
|
|
|
|
UNIX_EPOCH_IN_CLR_TICKS = 621355968000000000
|
|
|
|
CLR_TICKS_PER_SECOND = 10000000
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
DISCOVERY_TTL = 1.days
|
2016-08-03 21:31:25 +08:00
|
|
|
DISCOVERY_TTL.freeze
|
|
|
|
|
2016-09-22 22:18:57 +08:00
|
|
|
# For more explanation see this:
|
|
|
|
# http://stackoverflow.com/questions/11888053/
|
|
|
|
# convert-net-datetime-ticks-property-to-date-in-objective-c
|
|
|
|
def convert_to_unix_timestamp(timestamp)
|
2016-09-23 17:42:12 +08:00
|
|
|
Time.at((timestamp - UNIX_EPOCH_IN_CLR_TICKS) / CLR_TICKS_PER_SECOND)
|
2016-09-22 22:18:57 +08:00
|
|
|
end
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-22 22:18:57 +08:00
|
|
|
def get_action(extension, activity)
|
2016-09-23 17:42:12 +08:00
|
|
|
current_wopi_discovery
|
|
|
|
WopiAction.find_action(extension, activity)
|
2016-09-22 22:18:57 +08:00
|
|
|
end
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
def current_wopi_discovery
|
2016-09-22 22:18:57 +08:00
|
|
|
discovery = WopiDiscovery.first
|
|
|
|
return discovery if discovery && discovery.expires >= Time.now.to_i
|
|
|
|
initialize_discovery(discovery)
|
2016-08-03 21:31:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-09-22 22:18:57 +08:00
|
|
|
|
2016-08-03 21:31:25 +08:00
|
|
|
# Currently only saves Excel, Word and PowerPoint view and edit actions
|
2016-09-22 22:18:57 +08:00
|
|
|
def initialize_discovery(discovery)
|
2016-09-23 17:42:12 +08:00
|
|
|
Rails.logger.warn 'Initializing discovery'
|
|
|
|
discovery.destroy if discovery
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-30 15:59:03 +08:00
|
|
|
@doc = Nokogiri::XML(Kernel.open(ENV['WOPI_DISCOVERY_URL']))
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
discovery = WopiDiscovery.new
|
|
|
|
discovery.expires = Time.now.to_i + DISCOVERY_TTL
|
|
|
|
key = @doc.xpath('//proof-key')
|
|
|
|
discovery.proof_key_mod = key.xpath('@modulus').first.value
|
|
|
|
discovery.proof_key_exp = key.xpath('@exponent').first.value
|
|
|
|
discovery.proof_key_old_mod = key.xpath('@oldmodulus').first.value
|
|
|
|
discovery.proof_key_old_exp = key.xpath('@oldexponent').first.value
|
|
|
|
discovery.save!
|
2016-08-03 21:31:25 +08:00
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
@doc.xpath('//app').each do |app|
|
|
|
|
app_name = app.xpath('@name').first.value
|
|
|
|
next unless %w(Excel Word PowerPoint WopiTest).include?(app_name)
|
|
|
|
|
|
|
|
wopi_app = WopiApp.new
|
|
|
|
wopi_app.name = app.xpath('@name').first.value
|
|
|
|
wopi_app.icon = app.xpath('@favIconUrl').first.value
|
|
|
|
wopi_app.wopi_discovery_id = discovery.id
|
|
|
|
wopi_app.save!
|
|
|
|
app.xpath('action').each do |action|
|
|
|
|
name = action.xpath('@name').first.value
|
2019-04-30 14:19:52 +08:00
|
|
|
next unless %w(view edit editnew wopitest).include?(name)
|
|
|
|
|
2016-09-23 17:42:12 +08:00
|
|
|
wopi_action = WopiAction.new
|
|
|
|
wopi_action.action = name
|
|
|
|
wopi_action.extension = action.xpath('@ext').first.value
|
|
|
|
wopi_action.urlsrc = action.xpath('@urlsrc').first.value
|
|
|
|
wopi_action.wopi_app_id = wopi_app.id
|
|
|
|
wopi_action.save!
|
2016-08-03 21:31:25 +08:00
|
|
|
end
|
|
|
|
end
|
2016-09-23 17:42:12 +08:00
|
|
|
discovery
|
2016-09-30 15:59:03 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn 'WOPI: initialization failed: ' + e.message
|
|
|
|
e.backtrace.each { |line| Rails.logger.error line }
|
2016-09-23 17:42:12 +08:00
|
|
|
discovery = WopiDiscovery.first
|
|
|
|
discovery.destroy if discovery
|
2016-08-03 21:31:25 +08:00
|
|
|
end
|
2016-09-29 21:30:55 +08:00
|
|
|
|
|
|
|
def create_wopi_file_activity(current_user, started_editing)
|
2019-03-19 23:12:07 +08:00
|
|
|
action = if started_editing
|
|
|
|
t('activities.wupi_file_editing.started')
|
|
|
|
else
|
|
|
|
t('activities.wupi_file_editing.finished')
|
|
|
|
end
|
2016-09-29 21:30:55 +08:00
|
|
|
if @assoc.class == Step
|
2019-03-29 22:09:20 +08:00
|
|
|
default_step_items =
|
|
|
|
{ step: @asset.step.id,
|
2019-04-03 19:10:45 +08:00
|
|
|
step_position: { id: @asset.step.id, value_for: 'position_plus_one' },
|
2019-03-29 22:09:20 +08:00
|
|
|
asset_name: { id: @asset.id, value_for: 'file_file_name' },
|
|
|
|
action: action }
|
2016-09-29 21:30:55 +08:00
|
|
|
if @protocol.in_module?
|
2019-03-19 23:12:07 +08:00
|
|
|
project = @protocol.my_module.experiment.project
|
2019-04-04 01:51:54 +08:00
|
|
|
team = project.team
|
2019-03-29 22:09:20 +08:00
|
|
|
type_of = :edit_wopi_file_on_step
|
|
|
|
message_items = { my_module: @protocol.my_module.id }
|
2019-04-04 01:51:54 +08:00
|
|
|
else
|
|
|
|
type_of = :edit_wopi_file_on_step_in_repository
|
|
|
|
project = nil
|
|
|
|
team = @protocol.team
|
|
|
|
message_items = { protocol: @protocol.id }
|
2016-09-29 21:30:55 +08:00
|
|
|
end
|
2019-03-29 22:09:20 +08:00
|
|
|
message_items = default_step_items.merge(message_items)
|
2019-03-19 23:12:07 +08:00
|
|
|
Activities::CreateActivityService
|
2019-03-29 22:09:20 +08:00
|
|
|
.call(activity_type: type_of,
|
2019-03-19 23:12:07 +08:00
|
|
|
owner: current_user,
|
|
|
|
subject: @protocol,
|
2019-04-04 01:51:54 +08:00
|
|
|
team: team,
|
2019-03-19 23:12:07 +08:00
|
|
|
project: project,
|
2019-03-29 22:09:20 +08:00
|
|
|
message_items: message_items)
|
2018-05-21 21:48:48 +08:00
|
|
|
elsif @assoc.class == Result
|
2019-03-19 23:12:07 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :edit_wopi_file_on_result,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @asset.result,
|
|
|
|
team: @my_module.experiment.project.team,
|
|
|
|
project: @my_module.experiment.project,
|
|
|
|
message_items: {
|
|
|
|
result: @asset.result.id,
|
|
|
|
asset_name: { id: @asset.id, value_for: 'file_file_name' },
|
|
|
|
action: action
|
|
|
|
})
|
2016-09-29 21:30:55 +08:00
|
|
|
end
|
|
|
|
end
|
2016-09-22 22:18:57 +08:00
|
|
|
end
|