Class: Skydrive::FilesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/skydrive/files_controller.rb

Instance Method Summary collapse

Instance Method Details

#downloadObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/skydrive/files_controller.rb', line 46

def download
  api_key = ApiKey.where(access_token: params[:token]).first
  if api_key
    user = api_key.user
    uri = "#{user.token.personal_url}_api/Web/GetFileByServerRelativeUrl('#{params[:file].gsub(/ /, '%20')}')/$value"
    puts "URI: #{uri}"
    send_data open(uri, { "Authorization" => "Bearer #{user.token.access_token}"}, &:read)
  else
    render status: 401
  end
end

#ensure_valid_skydrive_tokenObject



8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/skydrive/files_controller.rb', line 8

def ensure_valid_skydrive_token
  unless current_user.valid_skydrive_token?
    head :unauthorized
    return
  end

  if current_user.token && current_user.token.not_before > Time.now
    current_user.token.refresh!(skydrive_client)
  end
end

#indexObject



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
# File 'app/controllers/skydrive/files_controller.rb', line 19

def index
  uri = params[:uri]
  uri = nil if uri == 'root' || uri == 'undefined'
  has_parent = true
  unless uri.present?
    personal_url = current_user.token.personal_url
    data = skydrive_client.api_call(personal_url + "_api/web/lists/Documents/")
    uri = data['RootFolder']['__deferred']['uri']
    has_parent = false
  end
  folder = skydrive_client.get_folder_and_files(uri)
  folder.parent_uri = nil unless has_parent

  tp = IMS::LTI::ToolProvider.new(nil, nil, current_api_key.params)
  tp.extend IMS::LTI::Extensions::Content::ToolProvider

  # Update the files with the correct homework submission url
  folder.files.each do |file|
    file.update_content_type_data( tp.accepted_file_extensions )
    file_download_url = download_url(token: current_api_key.access_token, file: file.server_relative_url)
    file.homework_submission_url = tp.file_content_return_url(file_download_url, file.name)
    file.is_embeddable = false unless tp.content_return_url.present?
  end

  render json: folder
end