Sha256: 501f36f0c3f98e79af343c0e780bc3770dc4a0d1aef6a608b8c31f23a7e60390

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# encoding: UTF-8

require 'fileutils'

module Spontaneous::Media
  class File
    F = ::File

    attr_reader :filename, :owner, :source

    def initialize(owner, filename, mimetype = nil)
      @owner, @filename, @mimetype = owner, Spontaneous::Media.to_filename(filename), mimetype
    end

    def rename(new_filename)
      self.class.new(owner, new_filename, nil)
    end

    def open(mode = 'wb', &block)
      storage.open(storage_path, mimetype, mode, &block)
    end

    def copy(existing_file)
      @source = existing_file.respond_to?(:path) ? existing_file.path : existing_file
      storage.copy(existing_file, storage_path, mimetype)
      self
    end

    def url
      storage.public_url(storage_path)
    end

    def mimetype
      @mimetype ||= ::Rack::Mime.mime_type(ext)
    end

    def ext
      F.extname(filename)
    end

    def filesize
      F.size(source)
    end

    def storage
      @storage ||= Spontaneous::Site.storage(mimetype)
    end

    def padded_id
      Spontaneous::Media.pad_id(owner.media_id)
    end

    def padded_revision
      Spontaneous::Media.pad_revision(revision)
    end

    def revision
      Spontaneous::Site.working_revision
    end

    def media_dir
      F.join(padded_id, padded_revision)
    end

    def storage_path
      [padded_id, padded_revision, filename]
    end

    def relative_path
      F.join(*storage_path)
    end

    def path
      F.join(storage.root, relative_path)
    end

    def dirname
      F.join(storage.root, media_dir)
    end

    alias_method :filepath, :path

    def serialize
      { :url => url, :mimetype => mimetype, :path => path, :filename => filename }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta1 lib/spontaneous/media/file.rb