Class: Arkaan::Campaigns::File

Inherits:
Document
  • Object
show all
Includes:
Arkaan::Concerns::MimeTypable
Defined in:
lib/arkaan/campaigns/file.rb

Overview

Representation of a file, allowing us to retrieve it on AWS by its filename and linked campaign ID.

Author:

Instance Attribute Summary collapse

Attributes inherited from Document

#campaign, #permission

Instance Method Summary collapse

Methods inherited from Document

#creator, #creator=, #has_permission?, #is_allowed?

Instance Attribute Details

#filenameString

Returns the name of the file, corresponding to the AWS name.

Returns:

  • (String)

    the name of the file, corresponding to the AWS name.



10
# File 'lib/arkaan/campaigns/file.rb', line 10

field :name, type: String

#sizeInteger

Returns the size of the file in bytes.

Returns:

  • (Integer)

    the size of the file in bytes.



13
# File 'lib/arkaan/campaigns/file.rb', line 13

field :size, type: Integer, default: 0

Instance Method Details

#exists_in_campaign(tmp_name) ⇒ Boolean

Checks if the given name already exists for a file in the campaign.

Parameters:

  • tmp_name (String)

    the name to check the existence of in the campaign.

Returns:

  • (Boolean)

    TRUE if the file exists in the campaign, FALSE otherwise.



48
49
50
# File 'lib/arkaan/campaigns/file.rb', line 48

def exists_in_campaign(tmp_name)
  campaign.files.where(name: tmp_name, :id.ne => id).exists?
end

#extensionString

Returns the extension of the file, without the dot.

Returns:

  • (String)

    the text representation for this file extension, without the leading dot.



60
61
62
# File 'lib/arkaan/campaigns/file.rb', line 60

def extension
  return self[:name].split('.').last
end

#merge_suffix(suffix) ⇒ String

Merges the given suffix with the name correctly.

Parameters:

  • suffix (String)

    the suffix to append between perenthesis at the end of the file name, but before the extension.

Returns:

  • (String)

    the complete name for the file, with the suffix correctly appended.



41
42
43
# File 'lib/arkaan/campaigns/file.rb', line 41

def merge_suffix(suffix)
  return "#{raw_name} (#{suffix}).#{extension}"
end

#name_unicityObject

Checks if the name is unique in the campaign, and if it is not then modifies it. The modifications are the same than in a windows or UNIX system :

  • if the name exists as it is given, the system adds the suffix “(1)” to it.

  • if the name with the suffix still exists, the system tries to increment it until an available name is found.

  • the found name is then afected to the name of the current file.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arkaan/campaigns/file.rb', line 26

def name_unicity
  if !campaign.nil? && !self[:name].nil?
    if exists_in_campaign(self[:name])
      suffix = 1
      while exists_in_campaign(merge_suffix(suffix))
        suffix = suffix + 1
      end
      self[:name] = merge_suffix(suffix)
    end
  end
end

#raw_nameString

Returns the file name without the extension for the current file.

Returns:

  • (String)

    the name of the file without the extension.



54
55
56
# File 'lib/arkaan/campaigns/file.rb', line 54

def raw_name
  return self[:name].gsub(".#{extension}", '')
end