Class: CarrierWave::Storage::AliyunFile

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/aliyun_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ AliyunFile

Returns a new instance of AliyunFile.



12
13
14
15
16
# File 'lib/carrierwave/storage/aliyun_file.rb', line 12

def initialize(uploader, base, path)
  @uploader = uploader
  @path = path
  @base = base
end

Instance Attribute Details

#fileObject



18
19
20
# File 'lib/carrierwave/storage/aliyun_file.rb', line 18

def file
  @file ||= bucket.get(path).try(:first)
end

#pathObject (readonly) Also known as: filename

Returns the value of attribute path.



7
8
9
# File 'lib/carrierwave/storage/aliyun_file.rb', line 7

def path
  @path
end

#uploaderObject (readonly)

Returns the value of attribute uploader.



7
8
9
# File 'lib/carrierwave/storage/aliyun_file.rb', line 7

def uploader
  @uploader
end

Instance Method Details

#content_typeObject



56
57
58
# File 'lib/carrierwave/storage/aliyun_file.rb', line 56

def content_type
  headers[:content_type]
end

#content_type=(new_content_type) ⇒ Object



60
61
62
# File 'lib/carrierwave/storage/aliyun_file.rb', line 60

def content_type=(new_content_type)
  headers[:content_type] = new_content_type
end

#copy_to(new_path) ⇒ Object



86
87
88
89
# File 'lib/carrierwave/storage/aliyun_file.rb', line 86

def copy_to(new_path)
  bucket.copy_object(path, new_path)
  self.class.new(uploader, @base, new_path)
end

#deleteObject



34
35
36
37
38
39
40
41
# File 'lib/carrierwave/storage/aliyun_file.rb', line 34

def delete
  bucket.delete(path)
  true
rescue StandardError => e
  # If the file's not there, don't panic
  puts "carrierwave-aliyun delete file failed: #{e}"
  nil
end

#exists?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/carrierwave/storage/aliyun_file.rb', line 82

def exists?
  !!headers
end

#extensionObject



91
92
93
94
# File 'lib/carrierwave/storage/aliyun_file.rb', line 91

def extension
  path_elements = path.split(".")
  path_elements.last if path_elements.size > 1
end

#headersObject



75
76
77
78
79
80
# File 'lib/carrierwave/storage/aliyun_file.rb', line 75

def headers
  @headers ||= begin
    obj = bucket.head(path)
    obj.headers
  end
end

#original_filenameObject



96
97
98
99
100
101
102
103
104
# File 'lib/carrierwave/storage/aliyun_file.rb', line 96

def original_filename
  return @original_filename if @original_filename

  if @file&.respond_to?(:original_filename)
    @file.original_filename
  elsif path
    ::File.basename(path)
  end
end

#readObject



28
29
30
31
32
# File 'lib/carrierwave/storage/aliyun_file.rb', line 28

def read
  object, body = bucket.get(path)
  @headers = object.headers
  body
end

#sizeObject



22
23
24
25
26
# File 'lib/carrierwave/storage/aliyun_file.rb', line 22

def size
  file.headers[:content_length].to_i
rescue StandardError
  nil
end

#store(new_file, headers = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/carrierwave/storage/aliyun_file.rb', line 64

def store(new_file, headers = {})
  if new_file.is_a?(self.class)
    new_file.copy_to(path)
  else
    fog_file = new_file.to_file
    bucket.put(path, fog_file, **headers)
    fog_file.close if fog_file && !fog_file.closed?
  end
  true
end

#url(opts = {}) ⇒ Object

Generate file url params

:thumb - Aliyun OSS Image Processor option, etc: @100w_200h_95q


48
49
50
51
52
53
54
# File 'lib/carrierwave/storage/aliyun_file.rb', line 48

def url(opts = {})
  if bucket.mode == :private
    bucket.private_get_url(path, **opts)
  else
    bucket.path_to_url(path, **opts)
  end
end