carrierwave-webdav 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 104e3949932f6d0d79ecc6e963ec20e22933ce97
4
- data.tar.gz: 3ef2f1fb1586bf186c5a9c44e7e1721beae7bffa
3
+ metadata.gz: fbbacc8489704bb68e0c181f4819f5e835b9df21
4
+ data.tar.gz: f170dd3e53ecd19028177d6b64b800fbd70c55a1
5
5
  SHA512:
6
- metadata.gz: 172a6e8b9adb899ecdb5fecaeb67e99518554ac49bab2e644b1f9266d8f1147f0a9c74e150f49511b4c096361756ed31d103de63ea42c5ce4aa56cabe378c491
7
- data.tar.gz: 2099b07cfcbf6264bfb83e780ce874c6aace8ad37eff9692b8346eeefc0fe163678813b45e78b32df44da29ec0034f55a58b93d2ea50dd6a405e0177ffa590ff
6
+ metadata.gz: b63df00595ec605460c02e9c34b6e7535d587afee7f80c7876ae0fdcd8236c2e20fe4125d7f061a82246fbf775f032b9e5ee27fcece8969683c771f2d3d93fe9
7
+ data.tar.gz: 38332a2224a0f1707dd12c3456aafa3cdb8c7e5808c13afbb05761e0b80a2b37da8ab4006d88b29f375c60bcd16b8cd43705f10e205177661f69eb003dcdb526
@@ -1 +1 @@
1
- repo_token: vED8szM0CKiJzSUuQmmbuuWStRxi3wMSV
1
+ repo_token: 9Fl9auxZBvbw6mQ2MDCN6HAs0qeBSANwg
data/Gemfile CHANGED
@@ -5,4 +5,4 @@ gemspec
5
5
  gem 'rake'
6
6
  gem 'rspec'
7
7
  gem 'webmock', :require => 'webmock/rspec'
8
- gem 'coveralls', require: false
8
+ gem 'coveralls', require: false
@@ -19,5 +19,4 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency 'carrierwave'
21
21
  gem.add_dependency 'httparty'
22
-
23
22
  end
@@ -1,8 +1,57 @@
1
1
  require 'carrierwave/httparty_monkey'
2
+ require 'carrierwave/webdav/file'
2
3
 
3
4
  module CarrierWave
4
5
  module Storage
5
6
  class WebDAV < Abstract
7
+ # Store file in WebDAV cache directory
8
+ #
9
+ # === Parameters
10
+ #
11
+ # [ file (CarrierWave::SanitizedFile) ] the file to store
12
+ #
13
+ # === Returns
14
+ #
15
+ # [ CarrierWave::WebDAV::File ] a sanitized file
16
+ #
17
+ def cache!(file)
18
+ cached = build_webdav_file(uploader.cache_path)
19
+ cached.write(file.read)
20
+ cached
21
+ end
22
+
23
+ # Retrieve file with given cache identifier from WebDAV
24
+ #
25
+ # === Parameters
26
+ #
27
+ # [ identifier (String) ] cache identifier
28
+ #
29
+ # === Returns
30
+ #
31
+ # [ CarrierWave::WebDAV::File ] a sanitized file
32
+ #
33
+ def retrieve_from_cache!(identifier)
34
+ build_webdav_file(uploader.cache_path(identifier))
35
+ end
36
+
37
+ # Delete cache directory from WebDAV
38
+ #
39
+ # === Parameters
40
+ #
41
+ # [ path (String) ] cache path
42
+ #
43
+ # === Returns
44
+ #
45
+ # [ HTTParty::Response ] httparty response object
46
+ #
47
+ # === Raises
48
+ #
49
+ # [ CarrierWave::IntegrityError ]
50
+ #
51
+ def delete_dir!(path)
52
+ cached = build_webdav_file(path)
53
+ cached.delete_dir
54
+ end
6
55
 
7
56
  # Store the file in WebDAV
8
57
  #
@@ -12,10 +61,10 @@ module CarrierWave
12
61
  #
13
62
  # === Returns
14
63
  #
15
- # [ CarrierWave::SanitizedFile ] a sanitized file
64
+ # [ CarrierWave::WebDAV::File ] a sanitized file
16
65
  #
17
66
  def store!(file)
18
- stored = CarrierWave::Storage::WebDAV::File.new(uploader, uploader.store_path)
67
+ stored = build_webdav_file(uploader.store_path)
19
68
  stored.write(file.read)
20
69
  stored
21
70
  end
@@ -28,113 +77,17 @@ module CarrierWave
28
77
  #
29
78
  # === Returns
30
79
  #
31
- # [ CarrierWave::Store::WebDAV::File ] a sanitized file
80
+ # [ CarrierWave::WebDAV::File ] a sanitized file
32
81
  #
33
82
  def retrieve!(identifier)
34
- CarrierWave::Storage::WebDAV::File.new(uploader, uploader.store_path(identifier))
83
+ build_webdav_file(uploader.store_path(identifier))
35
84
  end
36
85
 
37
- class File
38
- attr_reader :path
39
- attr_reader :uploader
40
- attr_reader :options
41
- attr_reader :server # Like 'https://www.WebDAV.com/dav'
42
-
43
- def initialize(uploader, path)
44
- @path = path
45
- @path.sub! /^\//, ''
46
- @uploader = uploader
47
- @server = uploader.webdav_server
48
- @server.sub! /\/$/, ''
49
- @write_server = uploader.webdav_write_server
50
- @write_server.sub!(/\/$/, '') if @write_server
51
- @username = uploader.webdav_username
52
- @password = uploader.webdav_password || ''
53
- @options = {}
54
- @options = { basic_auth: { username: @username, password: @password } } if @username
55
- @create_dirs = !uploader.webdav_autocreates_dirs
56
- end
57
-
58
- def read
59
- res = HTTParty.get(read_url, options)
60
- if res.code != 200
61
- raise CarrierWave::IntegrityError.new("Can't download a file: #{res.inspect}")
62
- end
63
- res.body
64
- end
65
-
66
- def headers
67
- res = HTTParty.head(read_url, options)
68
- if res.code != 200
69
- raise CarrierWave::IntegrityError.new("Can't headers for a file: #{res.inspect}")
70
- end
71
- res.headers
72
- end
73
-
74
- def write(file)
75
- if @create_dirs
76
- res = mkcol
77
- end
78
-
79
- res = HTTParty.put(write_url, options.merge({ body: file }))
80
- if res.code != 201 and res.code != 204
81
- raise CarrierWave::IntegrityError.new("Can't put a new file: #{res.inspect}")
82
- end
83
- res
84
- end
85
-
86
- def length
87
- read.bytesize
88
- end
89
-
90
- def content_type
91
- headers.content_type
92
- end
93
-
94
- def delete
95
- res = HTTParty.delete(write_url, options)
96
- if res.code != 200 and res.code != 204 and res.code != 404
97
- raise CarrierWave::IntegrityError.new("Can't delete a file: #{res.inspect}")
98
- end
99
- res
100
- end
101
-
102
- def url
103
- if host = uploader.asset_host
104
- host.respond_to?(:call) ? host.call(self) : [host, path].join('/')
105
- else
106
- read_url
107
- end
108
- end
109
-
110
- alias :content_length :length
111
- alias :file_length :length
112
- alias :size :length
113
-
114
86
  private
115
87
 
116
- def read_url
117
- "#{server}/#{path}"
118
- end
119
-
120
- def write_url
121
- @write_server ? "#{@write_server}/#{path}" : read_url
122
- end
123
-
124
- def mkcol
125
- dirs = []
126
- path.split('/')[0...-1].each do |dir|
127
- dirs << "#{dirs[-1]}/#{dir}"
128
- end # Make path like a/b/c/t.txt to array ['/a', '/a/b', '/a/b/c']
129
- use_server = @write_server ? @write_server : server
130
- dirs.each do |dir|
131
- res = HTTParty.mkcol("#{use_server}#{dir}", options)
132
- unless [200, 201, 207, 409].include? res.code
133
- raise CarrierWave::IntegrityError.new("Can't create a new collection: #{res.inspect}")
134
- end
135
- end # Make collections recursively
136
- end
137
- end # File
88
+ def build_webdav_file(path)
89
+ CarrierWave::WebDAV::File.new(uploader, path)
90
+ end
138
91
  end # WebDAV
139
92
  end # Storage
140
93
  end # CarrierWave
@@ -0,0 +1,110 @@
1
+ module CarrierWave
2
+ module WebDAV
3
+ class File
4
+ attr_reader :path
5
+ attr_reader :uploader
6
+ attr_reader :options
7
+ attr_reader :server # Like 'https://www.WebDAV.com/dav'
8
+
9
+ def initialize(uploader, path)
10
+ @path = path
11
+ @path.sub! /^\//, ''
12
+ @uploader = uploader
13
+ @server = uploader.webdav_server
14
+ @server.sub! /\/$/, ''
15
+ @write_server = uploader.webdav_write_server
16
+ @write_server.sub!(/\/$/, '') if @write_server
17
+ @username = uploader.webdav_username
18
+ @password = uploader.webdav_password || ''
19
+ @options = {}
20
+ @options = { basic_auth: { username: @username, password: @password } } if @username
21
+ @create_dirs = !uploader.webdav_autocreates_dirs
22
+ end
23
+
24
+ def read
25
+ res = HTTParty.get(read_url, options)
26
+ if res.code != 200
27
+ raise CarrierWave::IntegrityError.new("Can't download a file: #{res.inspect}")
28
+ end
29
+ res.body
30
+ end
31
+
32
+ def headers
33
+ res = HTTParty.head(read_url, options)
34
+ if res.code != 200
35
+ raise CarrierWave::IntegrityError.new("Can't headers for a file: #{res.inspect}")
36
+ end
37
+ res.headers
38
+ end
39
+
40
+ def write(file)
41
+ if @create_dirs
42
+ res = mkcol
43
+ end
44
+
45
+ res = HTTParty.put(write_url, options.merge({ body: file }))
46
+ if res.code != 201 and res.code != 204
47
+ raise CarrierWave::IntegrityError.new("Can't put a new file: #{res.inspect}")
48
+ end
49
+ res
50
+ end
51
+
52
+ def length
53
+ read.bytesize
54
+ end
55
+
56
+ def content_type
57
+ headers.content_type
58
+ end
59
+
60
+ def delete
61
+ res = HTTParty.delete(write_url, options)
62
+ if res.code != 200 and res.code != 204 and res.code != 404
63
+ raise CarrierWave::IntegrityError.new("Can't delete a file: #{res.inspect}")
64
+ end
65
+ res
66
+ end
67
+
68
+ def delete_dir
69
+ @path += '/' unless path.end_with?('/')
70
+ delete
71
+ end
72
+
73
+ def url
74
+ if host = uploader.asset_host
75
+ host.respond_to?(:call) ? host.call(self) : [host, path].join('/')
76
+ else
77
+ read_url
78
+ end
79
+ end
80
+
81
+ alias :content_length :length
82
+ alias :file_length :length
83
+ alias :size :length
84
+
85
+ private
86
+
87
+ def read_url
88
+ "#{server}/#{path}"
89
+ end
90
+
91
+ def write_url
92
+ @write_server ? "#{@write_server}/#{path}" : read_url
93
+ end
94
+
95
+ def mkcol
96
+ dirs = []
97
+ path.split('/')[0...-1].each do |dir|
98
+ dirs << "#{dirs[-1]}/#{dir}"
99
+ end # Make path like a/b/c/t.txt to array ['/a', '/a/b', '/a/b/c']
100
+ use_server = @write_server ? @write_server : server
101
+ dirs.each do |dir|
102
+ res = HTTParty.mkcol("#{use_server}#{dir}", options)
103
+ unless [200, 201, 207, 409].include? res.code
104
+ raise CarrierWave::IntegrityError.new("Can't create a new collection: #{res.inspect}")
105
+ end
106
+ end # Make collections recursively
107
+ end
108
+ end # File
109
+ end # WebDAV
110
+ end # CarrierWave
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module WebDAV
3
- VERSION = "0.4.3"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ Hello, this is tmp test data.
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::WebDAV::File do
4
+ describe '#url' do
5
+ let(:webdav_server) { 'https://your.webdavserver.com/dav/' }
6
+
7
+ before do
8
+ CarrierWave.configure do |config|
9
+ config.storage = :webdav
10
+ config.cache_storage = :webdav
11
+ config.webdav_server = webdav_server
12
+ config.asset_host = host
13
+
14
+ config.webdav_autocreates_dirs = true
15
+ end
16
+
17
+ @uploader = CarrierWave::Uploader::Base.new
18
+ @file = File.open(file_path('test.txt'))
19
+
20
+ stub_request(:put, %r{#{Regexp.escape(webdav_server)}}).to_return(status: 201)
21
+ end
22
+
23
+ context 'when asset_host is set' do
24
+ let(:host) { 'http://asset.host' }
25
+
26
+ it 'path contains asset_host' do
27
+ @uploader.cache!(@file)
28
+ expect(@uploader.file.url).to eq [host, @uploader.file.path].join('/')
29
+ end
30
+ end
31
+
32
+ context 'when asset_host is not set' do
33
+ let(:host) { nil }
34
+
35
+ it 'path does not contain asset_host' do
36
+ @uploader.cache!(@file)
37
+ expect(@uploader.file.url).to eq [webdav_server, @uploader.file.path].join('/')
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'carrierwave/webdav'
3
2
  require 'uri'
4
3
 
5
4
  describe CarrierWave::Storage::WebDAV do
@@ -13,15 +12,46 @@ describe CarrierWave::Storage::WebDAV do
13
12
  end
14
13
 
15
14
  @uploader = CarrierWave::Uploader::Base.new
16
- @uploader.stub store_path: 'uploads/test.txt'
15
+
16
+ allow(@uploader).to receive(:store_path) { 'uploads/test.txt' }
17
+ allow(@uploader).to receive(:cache_path) { 'uploads/tmp_test.txt' }
17
18
 
18
19
  @storage = CarrierWave::Storage::WebDAV.new(@uploader)
19
20
  @file = CarrierWave::SanitizedFile.new(file_path('test.txt'))
20
21
 
21
- @uri = URI(@uploader.webdav_server)
22
- @uri.user = @uploader.webdav_username
23
- @uri.password = @uploader.webdav_password
24
- @uri.merge! @uploader.store_path
22
+ # NOTE: specs fail with this options
23
+ #
24
+ # @uri.user = @uploader.webdav_username
25
+ # @uri.password = @uploader.webdav_password
26
+
27
+ @uri = URI(File.join(@uploader.webdav_server, @uploader.store_path))
28
+ @cache_uri = URI(File.join(@uploader.webdav_server, @uploader.cache_path))
29
+ end
30
+
31
+ describe '#cache!' do
32
+ it 'should cache from WebDAV' do
33
+ stub_mkcol @cache_uri
34
+ stub_put @cache_uri
35
+ webdav_file = @storage.cache!(@file)
36
+ stub_get @cache_uri
37
+ expect(@file.read).to eq(webdav_file.read)
38
+ end
39
+ end
40
+
41
+ describe '#retrieve_from_cache!' do
42
+ it 'should retreive a cache file from WebDAV' do
43
+ stub_get @cache_uri
44
+ webdav_file = @storage.retrieve_from_cache!('tmp_test.txt')
45
+ expect(@file.read).to eq(webdav_file.read)
46
+ end
47
+ end
48
+
49
+ describe '#delete_dir!' do
50
+ it 'should delete cache directory' do
51
+ stub_delete File.join(@uploader.webdav_server, 'uploads/')
52
+ result = @storage.delete_dir!('uploads')
53
+ expect(Net::HTTPOK).to eq(result.response.class)
54
+ end
25
55
  end
26
56
 
27
57
  it 'should store from WebDAV' do
@@ -73,51 +103,34 @@ describe CarrierWave::Storage::WebDAV do
73
103
  expect("#{@uploader.webdav_server}/#{@uploader.store_path}").to eq(webdav_file.url)
74
104
  end
75
105
 
76
- it 'should save through secure server' do
77
- CarrierWave.configure do |config|
78
- config.webdav_write_server = 'https://secure.your.webdavserver.com/dav/'
79
- end
80
-
81
- secure_uri = URI(@uploader.webdav_write_server)
82
- secure_uri.user = @uploader.webdav_username
83
- secure_uri.password = @uploader.webdav_password
84
- secure_uri.merge! @uploader.store_path
85
-
86
- stub_mkcol secure_uri
87
- stub_put secure_uri
88
- webdav_file = @storage.store!(@file)
89
-
90
- stub_get @uri
91
- expect(@file.read).to eq(webdav_file.read)
92
- end
93
-
94
- describe 'File#url' do
95
- let(:root) { Pathname.new(@file.path).dirname }
96
- let(:path) { @uploader.path.sub(root.to_path, '') }
97
-
106
+ context 'when use write server' do
98
107
  before do
99
108
  CarrierWave.configure do |config|
100
- config.asset_host = host
101
- config.root = root
109
+ config.webdav_write_server = 'https://secure.your.webdavserver.com/dav/'
102
110
  end
103
111
  end
104
112
 
105
- context 'when asset_host is set' do
106
- let(:host) { 'http://asset.host' }
107
-
108
- it 'path contains asset_host' do
109
- @uploader.cache!(@file)
110
- expect(@uploader.url).to eq [host, path].join
113
+ after do
114
+ CarrierWave.configure do |config|
115
+ config.webdav_write_server = nil
111
116
  end
112
117
  end
113
118
 
114
- context 'when asset_host is not set' do
115
- let(:host) { nil }
119
+ it 'should save through secure server' do
120
+ secure_uri = URI(File.join(@uploader.webdav_write_server, @uploader.store_path))
116
121
 
117
- it 'path does not contain asset_host' do
118
- @uploader.cache!(@file)
119
- expect(@uploader.url).to eq path
120
- end
122
+ # NOTE: specs fail with this options
123
+ #
124
+ # secure_uri.user = @uploader.webdav_username
125
+ # secure_uri.password = @uploader.webdav_password
126
+
127
+ stub_mkcol secure_uri
128
+ stub_put secure_uri
129
+ webdav_file = @storage.store!(@file)
130
+
131
+ stub_get @uri
132
+ expect(@file.read).to eq(webdav_file.read)
121
133
  end
122
134
  end
135
+
123
136
  end
@@ -4,6 +4,7 @@ require 'webmock/rspec'
4
4
  Dir['spec/supports/**/*.rb'].each { |f| require File.expand_path(f) }
5
5
 
6
6
  require 'carrierwave'
7
+ require 'carrierwave/webdav'
7
8
 
8
9
  if ENV['TRAVIS']
9
10
  require 'coveralls'
@@ -14,4 +15,3 @@ RSpec.configure do |config|
14
15
  WebMock.disable_net_connect!
15
16
  config.include Helpers
16
17
  end
17
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-webdav
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Qinix
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-30 00:00:00.000000000 Z
11
+ date: 2017-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -56,9 +56,12 @@ files:
56
56
  - lib/carrierwave/httparty_monkey.rb
57
57
  - lib/carrierwave/storage/webdav.rb
58
58
  - lib/carrierwave/webdav.rb
59
+ - lib/carrierwave/webdav/file.rb
59
60
  - lib/carrierwave/webdav/version.rb
60
61
  - spec/fixtures/test.txt
61
- - spec/lib/webdav_spec.rb
62
+ - spec/fixtures/tmp_test.txt
63
+ - spec/lib/webdav_file_spec.rb
64
+ - spec/lib/webdav_storage_spec.rb
62
65
  - spec/spec_helper.rb
63
66
  - spec/supports/helpers.rb
64
67
  homepage: https://github.com/qinix/carrierwave-webdav
@@ -86,7 +89,8 @@ specification_version: 4
86
89
  summary: WebDAV support for CarrierWave
87
90
  test_files:
88
91
  - spec/fixtures/test.txt
89
- - spec/lib/webdav_spec.rb
92
+ - spec/fixtures/tmp_test.txt
93
+ - spec/lib/webdav_file_spec.rb
94
+ - spec/lib/webdav_storage_spec.rb
90
95
  - spec/spec_helper.rb
91
96
  - spec/supports/helpers.rb
92
- has_rdoc: