Class: CarrierWave::Storage::QiniuFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, path) ⇒ QiniuFile

Returns a new instance of QiniuFile.



10
11
12
# File 'lib/carrierwave/storage/qiniu_file.rb', line 10

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

Instance Attribute Details

#copy_from_pathObject

Returns the value of attribute copy_from_path.



8
9
10
# File 'lib/carrierwave/storage/qiniu_file.rb', line 8

def copy_from_path
  @copy_from_path
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#uploaderObject (readonly)

Returns the value of attribute uploader.



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

def uploader
  @uploader
end

Instance Method Details

#content_typeObject



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

def content_type
  file_info["mimeType"] || "application/octet-stream".freeze
end

#copy_from(origin_path) ⇒ Boolean

Note:

从指定路径复制文件

Parameters:

  • origin_path (String)

    原文件路径

Returns:

  • (Boolean)


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

def copy_from(origin_path)
  qiniu_connection.copy(origin_path, @path)
end

#copy_to(new_path) ⇒ Boolean

Note:

复制文件到指定路径

Parameters:

  • new_path (String)

    新路径

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/carrierwave/storage/qiniu_file.rb', line 65

def copy_to(new_path)
  qiniu_connection.copy(@path, new_path)
  self.class.new(@uploader, new_path)
end

#deleteObject



42
43
44
# File 'lib/carrierwave/storage/qiniu_file.rb', line 42

def delete
  qiniu_connection.delete @path
end

#exists?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/carrierwave/storage/qiniu_file.rb', line 46

def exists?
  return true if qiniu_connection.stat(@path).present?
  false
end

#extensionObject



99
100
101
# File 'lib/carrierwave/storage/qiniu_file.rb', line 99

def extension
  @path.split(".").last
end

#filenameObject



103
104
105
# File 'lib/carrierwave/storage/qiniu_file.rb', line 103

def filename
  ::File.basename(@path)
end

#move_to(new_path) ⇒ Boolean

Note:

移动文件到指定路径

Parameters:

  • new_path (String)

    新路径

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/carrierwave/storage/qiniu_file.rb', line 75

def move_to(new_path)
  qiniu_connection.move(@path, new_path)
  self.class.new(@uploader, new_path)
end

#original_filenameObject



107
108
109
110
111
112
113
114
# File 'lib/carrierwave/storage/qiniu_file.rb', line 107

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

#readObject

Reads the contents of the file from Cloud Files

Returns

String

contents of the file



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

def read
  qiniu_connection.get(@path) if self.size > 0
end

#sizeObject



95
96
97
# File 'lib/carrierwave/storage/qiniu_file.rb', line 95

def size
  file_info["fsize"] || 0
end

#store(new_file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/carrierwave/storage/qiniu_file.rb', line 29

def store(new_file)
  if new_file.is_a?(self.class)
    if new_file.respond_to?(:copy_from_path) && new_file.copy_from_path.present?
      new_file.copy_from new_file.copy_from_path
    else
      new_file.copy_to @path
    end
  else
    qiniu_connection.upload_file(new_file.path, @path)
  end
  true
end

#url(options = {}) ⇒ Object

Return qiniu URl, maybe with style

Parameters

options (Hash)

optional options hash, 图片样式 { version: :thumb } 或者 { style: “imageView2/1/w/200” }

Returns

String


24
25
26
27
# File 'lib/carrierwave/storage/qiniu_file.rb', line 24

def url(options = {})
  the_path = options.present? ? path_with_style(options) : @path
  qiniu_connection.download_url(the_path)
end