forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvadoc.rb
More file actions
113 lines (94 loc) · 3.18 KB
/
Copy pathcanvadoc.rb
File metadata and controls
113 lines (94 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
class Canvadoc < ActiveRecord::Base
belongs_to :attachment
has_many :canvadocs_submissions
def upload(opts = {})
return if document_id.present?
url = attachment.authenticated_s3_url(expires_in: 1.day)
opts.delete(:annotatable) unless Canvadocs.annotations_supported?
response = Canvas.timeout_protection("canvadocs") {
canvadocs_api.upload(url, opts)
}
if response && response['id']
self.document_id = response['id']
self.process_state = response['status']
self.has_annotations = opts[:annotatable]
self.save!
elsif response.nil?
raise "no response received (request timed out?)"
else
raise response.inspect
end
end
def session_url(opts = {})
user = opts.delete(:user)
opts.merge! annotation_opts(user)
Canvas.timeout_protection("canvadocs", raise_on_timeout: true) do
session = canvadocs_api.session(document_id, opts)
canvadocs_api.view(session["id"])
end
end
def annotation_opts(user)
return {} if !user || !has_annotations?
opts = {
annotation_context: "default",
permissions: "readwrite",
user_id: user.global_id.to_s,
user_name: user.short_name.gsub(",", ""),
user_role: "",
user_filter: user.global_id.to_s,
}
return opts if submissions.empty?
if submissions.any? { |s| s.grants_right? user, :read_grade }
opts.delete :user_filter
end
# no commenting when anonymous peer reviews are enabled
if submissions.map(&:assignment).any? { |a| a.peer_reviews? && a.anonymous_peer_reviews? }
opts = {}
end
opts
end
private :annotation_opts
def submissions
self.canvadocs_submissions.
preload(submission: :assignment).
map &:submission
end
def available?
!!(document_id && process_state != 'error' && Canvadocs.enabled?)
end
def self.mime_types
JSON.parse Setting.get('canvadoc_mime_types', %w[
application/excel
application/msword
application/pdf
application/vnd.ms-excel
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.openxmlformats-officedocument.wordprocessingml.document
].to_json)
end
def canvadocs_api
raise "Canvadocs isn't enabled" unless Canvadocs.enabled?
Canvadocs::API.new(token: Canvadocs.config['api_key'],
base_url: Canvadocs.config['base_url'])
end
private :canvadocs_api
end