Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Update our fork #2

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
repo_token: vED8szM0CKiJzSUuQmmbuuWStRxi3wMSV
repo_token: 9Fl9auxZBvbw6mQ2MDCN6HAs0qeBSANwg
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: ruby
rvm:
- 1.9.3
- 2.2.2
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec
gem 'rake'
gem 'rspec'
gem 'webmock', :require => 'webmock/rspec'
gem 'coveralls', require: false
gem 'coveralls', require: false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CarrierWave for WebDAV

[![Build Status](https://travis-ci.org/qinix/carrierwave-webdav.png?branch=master)](https://travis-ci.org/qinix/carrierwave-webdav)
[![Coverage Status](https://coveralls.io/repos/github/qinix/carrierwave-webdav/badge.svg?branch=master)](https://coveralls.io/github/qinix/carrierwave-webdav?branch=master)

This gem adds support for WebDAV to
[CarrierWave](https://github.com/carrierwaveuploader/carrierwave/)
Expand Down Expand Up @@ -57,4 +58,3 @@ them yourself. In Rails for example, you could use the `send_data` method.
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

1 change: 0 additions & 1 deletion carrierwave-webdav.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ Gem::Specification.new do |gem|

gem.add_dependency 'carrierwave'
gem.add_dependency 'httparty'

end
163 changes: 56 additions & 107 deletions lib/carrierwave/storage/webdav.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
require 'carrierwave/httparty_monkey'
require 'carrierwave/webdav/file'

module CarrierWave
module Storage
class WebDAV < Abstract
# Store file in WebDAV cache directory
#
# === Parameters
#
# [ file (CarrierWave::SanitizedFile) ] the file to store
#
# === Returns
#
# [ CarrierWave::WebDAV::File ] a sanitized file
#
def cache!(file)
cached = build_webdav_file(uploader.cache_path)
cached.write(file.read)
cached
end

# Retrieve file with given cache identifier from WebDAV
#
# === Parameters
#
# [ identifier (String) ] cache identifier
#
# === Returns
#
# [ CarrierWave::WebDAV::File ] a sanitized file
#
def retrieve_from_cache!(identifier)
build_webdav_file(uploader.cache_path(identifier))
end

# Delete cache directory from WebDAV
#
# === Parameters
#
# [ path (String) ] cache path
#
# === Returns
#
# [ HTTParty::Response ] httparty response object
#
# === Raises
#
# [ CarrierWave::IntegrityError ]
#
def delete_dir!(path)
cached = build_webdav_file(path)
cached.delete_dir
end

# Store the file in WebDAV
#
Expand All @@ -12,10 +61,10 @@ class WebDAV < Abstract
#
# === Returns
#
# [ CarrierWave::SanitizedFile ] a sanitized file
# [ CarrierWave::WebDAV::File ] a sanitized file
#
def store!(file)
stored = CarrierWave::Storage::WebDAV::File.new(uploader, uploader.store_path)
stored = build_webdav_file(uploader.store_path)
stored.write(file.read)
stored
end
Expand All @@ -28,117 +77,17 @@ def store!(file)
#
# === Returns
#
# [ CarrierWave::Store::WebDAV::File ] a sanitized file
# [ CarrierWave::WebDAV::File ] a sanitized file
#
def retrieve!(identifier)
CarrierWave::Storage::WebDAV::File.new(uploader, uploader.store_path(identifier))
build_webdav_file(uploader.store_path(identifier))
end

class File
attr_reader :path
attr_reader :uploader
attr_reader :options
attr_reader :server # Like 'https://www.WebDAV.com/dav'

def initialize(uploader, path)
@path = path
@path.sub! /^\//, ''
@uploader = uploader
@server = uploader.webdav_server
@server.sub! /\/$/, ''
@write_server = uploader.webdav_write_server
@write_server.sub!(/\/$/, '') if @write_server
@username = uploader.webdav_username
@password = uploader.webdav_password || ''
@options = {}
@options = { basic_auth: { username: @username, password: @password } } if @username
end

def read
res = HTTParty.get(url, options)
if res.code != 200
raise CarrierWave::IntegrityError.new("Can't download a file: #{res.inspect}")
end
res.body
end

def headers
res = HTTParty.head(url, options)
if res.code != 200
raise CarrierWave::IntegrityError.new("Can't headers for a file: #{res.inspect}")
end
res.headers
end

def write(file)
mkcol

res = HTTParty.put(write_url, options.merge({ body: file }))
if res.code != 201 and res.code != 204
raise CarrierWave::IntegrityError.new("Can't put a new file: #{res.inspect}")
end
res
end

def length
read.bytesize
end

def content_type
headers.content_type
end

def delete
res = HTTParty.delete(write_url, options)
if res.code != 200 and res.code != 204 and res.code != 404
raise CarrierWave::IntegrityError.new("Can't delete a file: #{res.inspect}")
end
res
end

def url
"#{server}/#{path}"
end

alias :content_length :length
alias :file_length :length
alias :size :length

private

def write_url
@write_server ? "#{@write_server}/#{path}" : url
end

def mkcol
return if dirs_to_create.empty?


use_server = @write_server ? @write_server : server
dirs_to_create.each do |dir|

res = HTTParty.mkcol("#{use_server}#{dir}", options)
unless [200, 201, 207, 409].include? res.code
raise CarrierWave::IntegrityError.new("Can't create a new collection: #{res.inspect}")
end
end # Make collections recursively
end

def dirs_to_create
dirs = []
path.split('/')[0...-1].each do |dir|
dirs << "#{dirs[-1]}/#{dir}"
end
find_dirs_that_dont_exist(dirs)
end

def find_dirs_that_dont_exist dirs
dirs.reject{|dir|
res = HTTParty.propfind("#{server}/#{dir}", options)
[200,201,207].include? res.code
}
end
end # File
def build_webdav_file(path)
CarrierWave::WebDAV::File.new(uploader, path)
end
end # WebDAV
end # Storage
end # CarrierWave
122 changes: 122 additions & 0 deletions lib/carrierwave/webdav/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module CarrierWave
module WebDAV
class File
attr_reader :path
attr_reader :uploader
attr_reader :options
attr_reader :server # Like 'https://www.WebDAV.com/dav'

def initialize(uploader, path)
@path = path
@path.sub! /^\//, ''
@uploader = uploader
@server = uploader.webdav_server
@server.sub! /\/$/, ''
@write_server = uploader.webdav_write_server
@write_server.sub!(/\/$/, '') if @write_server
@username = uploader.webdav_username
@password = uploader.webdav_password || ''
@options = {}
@options = { basic_auth: { username: @username, password: @password } } if @username
end

def read
res = HTTParty.get(read_url, options)
if res.code != 200
raise CarrierWave::IntegrityError.new("Can't download a file: #{res.inspect}")
end
res.body
end

def headers
res = HTTParty.head(read_url, options)
if res.code != 200
raise CarrierWave::IntegrityError.new("Can't headers for a file: #{res.inspect}")
end
res.headers
end

def write(file)
mkcol

res = HTTParty.put(write_url, options.merge({ body: file }))
if res.code != 201 and res.code != 204
raise CarrierWave::IntegrityError.new("Can't put a new file: #{res.inspect}")
end
res
end

def length
read.bytesize
end

def content_type
headers.content_type
end

def delete
res = HTTParty.delete(write_url, options)
if res.code != 200 and res.code != 204 and res.code != 404
raise CarrierWave::IntegrityError.new("Can't delete a file: #{res.inspect}")
end
res
end

def delete_dir
@path += '/' unless path.end_with?('/')
delete
end

def url
if host = uploader.asset_host
host.respond_to?(:call) ? host.call(self) : [host, path].join('/')
else
read_url
end
end

alias :content_length :length
alias :file_length :length
alias :size :length

private

def read_url
"#{server}/#{path}"
end

def write_url
@write_server ? "#{@write_server}/#{path}" : read_url
end

def mkcol
return if dirs_to_create.empty?

use_server = @write_server ? @write_server : server

dirs_to_create.each do |dir|
res = HTTParty.mkcol("#{use_server}#{dir}", options)
unless [200, 201, 207, 409].include? res.code
raise CarrierWave::IntegrityError.new("Can't create a new collection: #{res.inspect}")
end
end # Make collections recursively
end

def dirs_to_create
dirs = []
path.split('/')[0...-1].each do |dir|
dirs << "#{dirs[-1]}/#{dir}"
end # Make path like a/b/c/t.txt to array ['/a', '/a/b', '/a/b/c']

find_dirs_that_dont_exist dirs
end

def find_dirs_that_dont_exist(dirs)
dirs.reject do |dir|
res = HTTParty.propfind("#{server}/#{dir}", options)
[200,201,207].include? res.code
end
end
end # File
end # WebDAV
end # CarrierWave
2 changes: 1 addition & 1 deletion lib/carrierwave/webdav/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CarrierWave
module WebDAV
VERSION = "0.4.1"
VERSION = "0.5.0"
end
end
1 change: 1 addition & 0 deletions spec/fixtures/tmp_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, this is tmp test data.
Loading