carrierwave-sharefile 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36855a70fa6605d2ca3f4aec029e0731cb7ff992
4
+ data.tar.gz: cc1141a0b4718d53965ea0f2250ebae47e680d11
5
+ SHA512:
6
+ metadata.gz: 870d06239cab36852e274a36cd132c96ee5a0d083bb1eac750af9c1b33061c228d7439ed7a5f0e4413c765d6225a81a788fa2e44e347afe2d748e948a632b8d1
7
+ data.tar.gz: 66921bd96f4ff4de640e90acc3ff26cfea00bb32de8d61183eb964dcd7547841bb6d2478a6bea1839e03ffb01fae8cd92aa8108b84a813ca8a38bca766cf9d76
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-truevault.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Matthew McFarling
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Carrierwave::Sharefile
2
+
3
+ This gem adds support for [sharefile](https://sharefile.com) to the CarrierWave.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-sharefile'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrierwave-sharefile
18
+
19
+ ## Usage
20
+
21
+ You'll need to add a configuration block in your ```
22
+ config/initializers/carrierwave.rb ``` file.
23
+
24
+ ```ruby
25
+ CarrierWave.configure do |config|
26
+ config.sharefile_client_id = ''
27
+ config.sharefile_client_secret = ''
28
+ config.sharefile_username = ''
29
+ config.sharefile_password = ''
30
+ end
31
+ ```
32
+
33
+ In your uploader add sharefile as the storage option:
34
+
35
+ ```ruby
36
+ class DocumentUploader < CarrierWave::Uploader::Base
37
+ storage :sharefile
38
+ end
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/codemancode/carrierwave-sharefile/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/sharefile/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-sharefile"
8
+ spec.version = Carrierwave::Sharefile::VERSION
9
+ spec.authors = ["Matthew McFarling"]
10
+ spec.email = ["matt@codemancode.com"]
11
+ spec.summary = %q{Sharefile integration for Carrierwave.}
12
+ spec.description = %q{Carrierwave storage for sharefile.}
13
+ spec.homepage = "http://www.codemancode.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "carrierwave"
22
+ spec.add_dependency "faraday"
23
+ spec.add_dependency "faraday_middleware"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,17 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/sharefile/client'
3
+ require 'carrierwave/storage/sharefile'
4
+ require "carrierwave/sharefile/version"
5
+
6
+ class CarrierWave::Uploader::Base
7
+ add_config :sharefile_client_id
8
+ add_config :sharefile_client_secret
9
+ add_config :sharefile_username
10
+ add_config :sharefile_password
11
+ add_config :sharefile_subdomain
12
+ add_config :sharefile_root
13
+
14
+ configure do |config|
15
+ config.storage_engines[:sharefile] = 'CarrierWave::Storage::Sharefile'
16
+ end
17
+ end
@@ -0,0 +1,115 @@
1
+ require 'faraday'
2
+
3
+ module CarrierWave
4
+ module Sharefile
5
+ class Client
6
+ require 'faraday'
7
+ require 'faraday_middleware'
8
+ require 'json'
9
+ require 'uri'
10
+ require 'open-uri'
11
+ require 'tempfile'
12
+
13
+ def initialize(client_id, client_secret, username, password, subdomain)
14
+ @client_id = client_id
15
+ @client_secret = client_secret
16
+ @username = username
17
+ @password = password
18
+ @subdomain = subdomain
19
+ instance_variables.each do |variable|
20
+ raise ArgumentError, "#{variable} should not be nil or blank" if instance_variable_get(variable.to_sym).to_s == ""
21
+ end
22
+ access_token
23
+ end
24
+
25
+ def access_token
26
+ params = {
27
+ :grant_type => :password,
28
+ :client_id => @client_id,
29
+ :client_secret => @client_secret,
30
+ :username => @username,
31
+ :password => @password
32
+ }
33
+ response = connection("sharefile").post 'oauth/token', params
34
+ @access_token = response.body['access_token']
35
+ @refresh_token = response.body['refresh_token']
36
+ end
37
+
38
+
39
+ def get_document(identifier)
40
+ response = get_item_by_id(identifier)
41
+ end
42
+
43
+ def get_download_link(path)
44
+ headers = {"Authorization" => "Bearer #{@access_token}"}
45
+ res = get_item_by_path(path)
46
+ id = res.body["Id"]
47
+ response = connection.get "sf/v3/Items(#{id})/Download", {}, headers
48
+ if response.headers['location']
49
+ return response.headers['location']
50
+ end
51
+ end
52
+
53
+ def store_document(root_folder, store_path, file)
54
+ folder = get_item_by_path(root_folder)
55
+ upload_config = upload_file_to_folder(folder)
56
+ res = upload_media(upload_config.body['ChunkUri'], file)
57
+ end
58
+
59
+ private
60
+
61
+ def upload_media(url, tmpfile)
62
+ newline = "\r\n"
63
+ filename = File.basename(tmpfile.path)
64
+ boundary = "ClientTouchReceive----------#{Time.now.usec}"
65
+
66
+ uri = URI.parse(url)
67
+
68
+ post_body = []
69
+ post_body << "--#{boundary}#{newline}"
70
+ post_body << "Content-Disposition: form-data; name=\"File1\"; filename=\"#{filename}\"#{newline}"
71
+ post_body << "Content-Type: application/octet-stream#{newline}"
72
+ post_body << "#{newline}"
73
+ post_body << File.read(tmpfile.path)
74
+ post_body << "#{newline}--#{boundary}--#{newline}"
75
+
76
+ request = Net::HTTP::Post.new(uri.request_uri)
77
+ request.body = post_body.join
78
+ request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
79
+ request['Content-Length'] = request.body().length
80
+
81
+ http = Net::HTTP.new uri.host, uri.port
82
+ http.use_ssl = true
83
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
84
+
85
+ response = http.request request
86
+ return {:response => response, :id => filename}
87
+ end
88
+
89
+ def upload_file_to_folder(folder)
90
+ headers = {"Authorization" => "Bearer #{@access_token}"}
91
+ body = {:method => 'standard', :fileName => 'testitout', :title => 'test upload', :details => 'test description'}
92
+ response = connection.post "sf/v3/Items(#{folder.body['Id']})/Upload", body, headers
93
+ end
94
+
95
+ def get_item_by_path(path = '/')
96
+ headers = {"Authorization" => "Bearer #{@access_token}"}
97
+ response = connection.get "sf/v3/Items/ByPath?path=#{path}", {}, headers
98
+ end
99
+
100
+ def get_item_by_id(identifier)
101
+ headers = {"Authorization" => "Bearer #{@access_token}"}
102
+ response = connection.get "sf/v3/Items/(#{identifier})?includeDeleted=false", {}, headers
103
+ end
104
+
105
+ def connection(endpoint = "sf-api")
106
+ Faraday.new(:url => "https://#{@subdomain}.#{endpoint}.com/") do |faraday|
107
+ faraday.request :url_encoded # form-encode POST params
108
+ faraday.use FaradayMiddleware::ParseJson
109
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
110
+ end
111
+ end
112
+
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module Sharefile
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,139 @@
1
+ require 'pathname'
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class Sharefile < Abstract
6
+ def store!(file)
7
+ f = CarrierWave::Storage::Sharefile::File.new(uploader, config, uploader.store_path, client)
8
+ f.store(file)
9
+ f
10
+ end
11
+
12
+ def retrieve!(identifier)
13
+ f = CarrierWave::Storage::Sharefile::File.new(uploader, config, uploader.store_path(identifier), client)
14
+ f.retrieve(identifier)
15
+ f
16
+ end
17
+
18
+ def client
19
+ CarrierWave::Sharefile::Client.new(config[:sharefile_client_id],
20
+ config[:sharefile_client_secret],
21
+ config[:sharefile_username],
22
+ config[:sharefile_password],
23
+ config[:sharefile_subdomain])
24
+ end
25
+
26
+ private
27
+
28
+ def config
29
+ @config ||= {}
30
+
31
+ @config[:sharefile_client_id] ||= uploader.sharefile_client_id
32
+ @config[:sharefile_client_secret] ||= uploader.sharefile_client_secret
33
+ @config[:sharefile_username] ||= uploader.sharefile_username
34
+ @config[:sharefile_password] ||= uploader.sharefile_password
35
+ @config[:sharefile_subdomain] ||= uploader.sharefile_subdomain
36
+ @config[:sharefile_root] ||= uploader.sharefile_root
37
+
38
+ @config
39
+ end
40
+
41
+ class File
42
+ include CarrierWave::Utilities::Uri
43
+ attr_reader :path
44
+
45
+ def initialize(uploader, config, path, client)
46
+ @uploader, @config, @path, @client = uploader, config, path, client
47
+ end
48
+
49
+ def filename
50
+ Pathname.new(path).basename.to_s
51
+ end
52
+
53
+ ##
54
+ # Lookup URL for the path
55
+ #
56
+ # === Returns
57
+ #
58
+ # [String] URL of the download link
59
+ #
60
+ def url
61
+ @client.get_download_link(@path)
62
+ end
63
+
64
+ ##
65
+ # Lookup value for file content-type header
66
+ #
67
+ # === Returns
68
+ #
69
+ # [String] value of content-type
70
+ #
71
+ def content_type
72
+ @content_type || file.content_type
73
+ end
74
+
75
+ ##
76
+ # Set non-default content-type header (default is file.content_type)
77
+ #
78
+ # === Returns
79
+ #
80
+ # [String] returns new content type value
81
+ #
82
+ def content_type=(new_content_type)
83
+ @content_type = new_content_type
84
+ end
85
+
86
+ ##
87
+ # Read content of file from service
88
+ #
89
+ # === Returns
90
+ #
91
+ # [String] contents of file
92
+ def read
93
+ file
94
+ end
95
+
96
+ ##
97
+ # Return size of file body
98
+ #
99
+ # === Returns
100
+ #
101
+ # [Integer] size of file body
102
+ #
103
+ def size
104
+ file.content_length
105
+ end
106
+
107
+ def sharefile_attributes
108
+ attributes
109
+ end
110
+
111
+ ##
112
+ # Write file to service
113
+ #
114
+ # === Returns
115
+ #
116
+ # [Boolean] true on success or raises error
117
+ #
118
+ def store(file)
119
+ sharefile_file = file.to_file
120
+ @content_type ||= file.content_type
121
+ root_folder = @config[:sharefile_root]
122
+ @file = @client.store_document(root_folder, @path, sharefile_file)
123
+ end
124
+
125
+ ##
126
+ # Retrieve file from service
127
+ #
128
+ # === Returns
129
+ #
130
+ # [File]
131
+ #
132
+ def retrieve(identifier)
133
+ @file = @client.get_document(identifier)
134
+ @file ||= @file.parsed_response
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-sharefile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew McFarling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Carrierwave storage for sharefile.
84
+ email:
85
+ - matt@codemancode.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - carrierwave-sharefile.gemspec
96
+ - lib/carrierwave/sharefile.rb
97
+ - lib/carrierwave/sharefile/client.rb
98
+ - lib/carrierwave/sharefile/version.rb
99
+ - lib/carrierwave/storage/sharefile.rb
100
+ homepage: http://www.codemancode.com
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.6
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Sharefile integration for Carrierwave.
124
+ test_files: []