Class: Cloudsync::File

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudsync/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
14
# File 'lib/cloudsync/file.rb', line 5

def initialize(options = {})
  @path                = options[:path] 
  @size                = options[:size]
  @last_modified       = options[:last_modified]
  @e_tag               = options[:e_tag]
  @backend             = options[:backend]
  @upload_prefix       = options[:upload_prefix]
  @download_prefix     = options[:download_prefix]
  @backend_type        = options[:backend_type]
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



3
4
5
# File 'lib/cloudsync/file.rb', line 3

def backend
  @backend
end

#e_tagObject

Returns the value of attribute e_tag.



3
4
5
# File 'lib/cloudsync/file.rb', line 3

def e_tag
  @e_tag
end

#last_modifiedObject

Returns the value of attribute last_modified.



3
4
5
# File 'lib/cloudsync/file.rb', line 3

def last_modified
  @last_modified
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/cloudsync/file.rb', line 3

def path
  @path
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/cloudsync/file.rb', line 3

def size
  @size
end

Class Method Details

.from_cf_info(container, path, hash, backend) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudsync/file.rb', line 28

def self.from_cf_info(container, path, hash, backend)
  new({ 
        :upload_prefix => container.name,
        :path          => path,
        :size          => hash[:bytes],
        :last_modified => hash[:last_modified].to_gm_time.to_i,
        :e_tag         => hash[:hash],
        :backend       => backend,
        :backend_type  => Cloudsync::Backend::CloudFiles })
end

.from_cf_obj(obj, backend = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloudsync/file.rb', line 39

def self.from_cf_obj(obj, backend=nil)
  return nil if obj.nil?
  new({
    :upload_prefix => obj.container.name,
    :path          => obj.name,
    :size          => obj.bytes.to_i,
    :last_modified => obj.last_modified.to_i,
    :e_tag         => obj.etag,
    :backend       => backend,
    :backend_type  => Cloudsync::Backend::CloudFiles})
end

.from_s3_obj(obj, backend = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cloudsync/file.rb', line 16

def self.from_s3_obj(obj, backend=nil)
  return nil if obj.nil?
  new({
    :upload_prefix => obj.bucket.name,
    :path          => obj.name,
    :size          => obj.size,
    :last_modified => obj.last_modified.to_i,
    :e_tag         => obj.e_tag.gsub('"',''),
    :backend       => backend,
    :backend_type  => Cloudsync::Backend::S3})
end

Instance Method Details

#bucketObject Also known as: container



63
64
65
66
67
# File 'lib/cloudsync/file.rb', line 63

def bucket
  @bucket ||= begin
    @upload_prefix.split("/").first
  end
end

#download_pathObject



79
80
81
# File 'lib/cloudsync/file.rb', line 79

def download_path
  @download_prefix ? "#{@download_prefix}/#{@path}" : @path
end

#full_download_pathObject



87
88
89
# File 'lib/cloudsync/file.rb', line 87

def full_download_path
  [bucket, download_path].join("/")
end

#full_nameObject



59
60
61
# File 'lib/cloudsync/file.rb', line 59

def full_name
  [bucket,path].join("/")
end

#full_upload_pathObject



91
92
93
# File 'lib/cloudsync/file.rb', line 91

def full_upload_path
  [bucket, upload_path].join("/")
end

#tempfileObject



95
96
97
# File 'lib/cloudsync/file.rb', line 95

def tempfile
  Tempfile.new(unique_filename)
end

#to_sObject



51
52
53
# File 'lib/cloudsync/file.rb', line 51

def to_s
  "#{full_upload_path}"
end

#unique_filenameObject



55
56
57
# File 'lib/cloudsync/file.rb', line 55

def unique_filename
  [bucket,e_tag,path].join.gsub(/[^a-zA-Z\-_0-9]/,'')
end

#upload_pathObject



70
71
72
73
74
75
76
77
# File 'lib/cloudsync/file.rb', line 70

def upload_path
  without_bucket_path = @upload_prefix.sub(/^#{bucket}\/?/,"")
  if without_bucket_path.empty?
    @path
  else
    without_bucket_path + "/" + @path
  end
end

#upload_path_without_bucketObject



83
84
85
# File 'lib/cloudsync/file.rb', line 83

def upload_path_without_bucket
  
end