Module: LayerVault::Client::Files

Included in:
LayerVault::Client
Defined in:
lib/layervault/client/files.rb

Instance Method Summary collapse

Instance Method Details

#create_file(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



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
# File 'lib/layervault/client/files.rb', line 17

def create_file(organization_name, project_name, path, file_name, options={} )
  raise ClientParamsError.new("You must specify the local_file_path option to the file you want to upload.") unless options.fetch(:local_file_path, nil)
  raise ClientParamsError.new("You must specify the content_type option to the content type of the file you are uploading.") unless options.fetch(:content_type, nil)

  local_file_path = options.fetch(:local_file_path, nil)
  content_type    = options.fetch(:content_type, nil)

  md5 = Digest::MD5.hexdigest(::File.read(local_file_path))
  options = {md5: md5}

  json_response = put("#{organization_name}/#{project_name}/#{path}/#{file_name}", options)

  s3_response = MultiJson.decode(json_response)
  s3_response.merge!( "Content-Type" => content_type)

  payload = s3_response.merge({ file: Faraday::UploadIO.new(local_file_path, content_type) })

  conn = Faraday.new('https://omnivore-scratch.s3.amazonaws.com') do |f|
    f.request :multipart
    f.request :url_encoded
    f.adapter :net_http # This is what ended up making it work
  end

  response_from_s3 = conn.post('/', payload)
  redirect = response_from_s3[:location]

  # Add the access_token to the query
  uri = URI.parse(redirect)
  new_query_ar = URI.decode_www_form(uri.query) << ["access_token", LayerVault.client.access_token]
  uri.query = URI.encode_www_form(new_query_ar)

  re = conn.post( uri.to_s )
end

#delete_file(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



12
13
14
15
# File 'lib/layervault/client/files.rb', line 12

def delete_file(organization_name, project_name, path, file_name, options={})
  raise ClientParamsError.new("You must specify the md5 option of the file you are trying to delete.") unless options.fetch(:md5, nil)
  delete "#{organization_name}/#{project_name}/#{path}/#{file_name}", options
end

#file(organization_name, project_name, path, file_name) ⇒ Object



8
9
10
# File 'lib/layervault/client/files.rb', line 8

def file(organization_name, project_name, path, file_name)
  get "#{organization_name}/#{project_name}/#{path}/#{file_name}"
end

#move_file(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



51
52
53
54
55
56
# File 'lib/layervault/client/files.rb', line 51

def move_file(organization_name, project_name, path, file_name, options={})
  raise ClientParamsError.new("You must specify the new_folder option to specify the destination folder") unless options.fetch(:new_folder, nil)
  raise ClientParamsError.new("You must specify the new_filename option to specify the destination folder") unless options.fetch(:new_file_name, nil)

  post "#{organization_name}/#{project_name}/#{path}/#{file_name}/move", options
end

#previews(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



63
64
65
66
# File 'lib/layervault/client/files.rb', line 63

def previews(organization_name, project_name, path, file_name, options={})
  raise ClientParamsError.new("You must specify the :w (width) and :h (height) options for the previews.") unless options.fetch(:w, nil) && options.fetch(:h, nil)
  get "#{organization_name}/#{project_name}/#{path}/#{file_name}/previews", options
end

#revisions(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



68
69
70
71
# File 'lib/layervault/client/files.rb', line 68

def revisions(organization_name, project_name, path, file_name, options={})
  raise ClientParamsError.new("You must specify the :first_seen or :latest_seen option for the file.") if options.fetch(:first_seen, nil) && options.fetch(:latest_seen, nil)
  get "#{organization_name}/#{project_name}/#{path}/#{file_name}/revisions", options
end

#sync_check(organization_name, project_name, path, file_name, options = {}) ⇒ Object

Raises:



58
59
60
61
# File 'lib/layervault/client/files.rb', line 58

def sync_check(organization_name, project_name, path, file_name, options={})
  raise ClientParamsError.new("You must specify the md5 option of the file you are trying to sync check.") unless options.fetch(:md5, nil)
  get "#{organization_name}/#{project_name}/#{path}/#{file_name}/sync_check", options
end