Class: Milton::Storage::DiskFile

Inherits:
StoredFile show all
Defined in:
lib/milton/storage/disk_file.rb

Overview

How Milton deals with a file stored on disk…

Instance Attribute Summary

Attributes inherited from StoredFile

#filename, #id, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StoredFile

adapter, #clone, create, #initialize, sanitize_filename

Constructor Details

This class inherits a constructor from Milton::Storage::StoredFile

Class Method Details

.recreate_directory(directory, options) ⇒ Object

Creates the given directory and sets it to the mode given in options



10
11
12
13
14
# File 'lib/milton/storage/disk_file.rb', line 10

def recreate_directory(directory, options)
  return true if File.exists?(directory)
  FileUtils.mkdir_p(directory)
  File.chmod(options[:storage_options][:chmod], directory)
end

Instance Method Details

#copy(destination) ⇒ Object

Copies this file to the given location on disk.



50
51
52
53
# File 'lib/milton/storage/disk_file.rb', line 50

def copy(destination)
  Milton.log "copying #{path} to #{destination}"
  FileUtils.cp(path, destination)
end

#destroyObject

Removes the file from the underlying file system and any derivatives of the file.



44
45
46
47
# File 'lib/milton/storage/disk_file.rb', line 44

def destroy
  Milton.log "destroying path #{dirname}"
  FileUtils.rm_rf dirname if File.exists?(dirname)
end

#dirnameObject

Returns the full directory path up to the file, w/o the filename.



25
26
27
# File 'lib/milton/storage/disk_file.rb', line 25

def dirname
  File.join(root_path, partitioned_path)
end

#exists?Boolean

Returns true if the file exists on the underlying file system.

Returns:

  • (Boolean)


30
31
32
# File 'lib/milton/storage/disk_file.rb', line 30

def exists?
  File.exist?(path)
end

#mime_typeObject

Returns the mime type of the file.



56
57
58
# File 'lib/milton/storage/disk_file.rb', line 56

def mime_type
  Milton.syscall("file -Ib #{path}").gsub(/\n/,"")
end

#pathObject

Returns the full path and filename to the file with the given options. If no options are given then returns the path and filename to the original file.



20
21
22
# File 'lib/milton/storage/disk_file.rb', line 20

def path
  File.join(dirname, filename)
end

#store(source) ⇒ Object

Writes the given source file to this file’s path.



35
36
37
38
39
40
# File 'lib/milton/storage/disk_file.rb', line 35

def store(source)
  Milton.log "storing #{source} to disk at #{path}"
  self.class.recreate_directory(dirname, options)
  File.cp(source, path)
  File.chmod(options[:storage_options][:chmod], path)
end