Class: Gollum::File

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki) ⇒ File

Public: Initialize a file.

wiki - The Gollum::Wiki in question.

Returns a newly initialized Gollum::File.



12
13
14
15
16
17
18
# File 'lib/gollum-lib/file.rb', line 12

def initialize(wiki)
  @wiki         = wiki
  @blob         = nil
  @path         = nil
  @on_disk      = false
  @on_disk_path = nil
end

Instance Attribute Details

#pathObject (readonly)

Public: The String path of the file.



79
80
81
# File 'lib/gollum-lib/file.rb', line 79

def path
  @path
end

#versionObject

Public: The Gollum::Git::Commit version of the file.



76
77
78
# File 'lib/gollum-lib/file.rb', line 76

def version
  @version
end

Instance Method Details

#escaped_url_pathObject

Public: The url_path, but CGI escaped.

Returns the String url_path



32
33
34
# File 'lib/gollum-lib/file.rb', line 32

def escaped_url_path
  CGI.escape(self.url_path).gsub('%2F', '/')
end

#find(name, version, try_on_disk = false) ⇒ Object

Find a file in the given Gollum repo.

name - The full String path. version - The String version ID to find. try_on_disk - If true, try to return just a reference to a file

that exists on the disk.

Returns a Gollum::File or nil if the file could not be found. Note that if you specify try_on_disk=true, you may or may not get a file for which on_disk? is actually true.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gollum-lib/file.rb', line 138

def find(name, version, try_on_disk = false)
  checked = name.downcase
  map     = @wiki.tree_map_for(version)
  commit  = version.is_a?(Gollum::Git::Commit) ? version : @wiki.commit_for(version)

  if (result = map.detect { |entry| entry.path.downcase == checked })
    @path    = name
    @version = commit

    if try_on_disk && get_disk_reference(name, commit)
      @on_disk = true
    else
      @blob = result.blob(@wiki.repo)
    end

    self
  end
end

#get_disk_reference(name, commit) ⇒ Object

Return the file path to this file on disk, if available.

Returns nil if the file isn’t available on disk. This can occur if the repo is bare, if the commit isn’t the HEAD, or if there are problems resolving symbolic links.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gollum-lib/file.rb', line 111

def get_disk_reference(name, commit)
  return false if @wiki.repo.bare
  return false if commit.sha != @wiki.repo.head.commit.sha

  # This will try to resolve symbolic links, as well
  pathname = Pathname.new(::File.expand_path(::File.join(@wiki.repo.path, '..', name)))
  if pathname.symlink?
    source   = ::File.readlink(pathname.to_path)
    realpath = ::File.join(::File.dirname(pathname.to_path), source)
    return false unless realpath && ::File.exist?(realpath)
    @on_disk_path = realpath.to_s
  else
    @on_disk_path = pathname.to_path
  end
  true
end

#mime_typeObject

Public: The String mime type of the file.



82
83
84
# File 'lib/gollum-lib/file.rb', line 82

def mime_type
  @blob && @blob.mime_type
end

#nameObject Also known as: filename

Public: The on-disk filename of the file.

Returns the String name.



39
40
41
42
# File 'lib/gollum-lib/file.rb', line 39

def name
  return @path if on_disk?
  @blob && @blob.name
end

#on_disk?Boolean

Public: Is this an on-disk file reference?

Returns true if this is a pointer to an on-disk file

Returns:

  • (Boolean)


64
65
66
# File 'lib/gollum-lib/file.rb', line 64

def on_disk?
  @on_disk
end

#on_disk_pathObject

Public: The path to this file on disk

Returns nil if on_disk? is false.



71
72
73
# File 'lib/gollum-lib/file.rb', line 71

def on_disk_path
  @on_disk_path
end

#populate(blob, path = nil) ⇒ Object

Populate the File with information from the Blob.

blob - The Gollum::Git::Blob that contains the info. path - The String directory path of the file.

Returns the populated Gollum::File.



92
93
94
95
96
97
98
# File 'lib/gollum-lib/file.rb', line 92

def populate(blob, path = nil)
  @blob         = blob
  @path         = "#{path}/#{blob.name}"[1..-1]
  @on_disk      = false
  @on_disk_path = nil
  self
end

#raw_dataObject

Public: The raw contents of the page.

Returns the String data.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gollum-lib/file.rb', line 49

def raw_data
  return IO.read(@on_disk_path) if on_disk?
  return nil unless @blob

  if !@wiki.repo.bare && @blob.is_symlink
    new_path = @blob.symlink_target(::File.join(@wiki.repo.path, '..', self.path))
    return IO.read(new_path) if new_path
  end

  @blob.data
end

#url_pathObject

Public: The url path required to reach this page within the repo.

Returns the String url_path



23
24
25
26
27
# File 'lib/gollum-lib/file.rb', line 23

def url_path
  path = self.path
  path = path.sub(/\/[^\/]+$/, '/') if path.include?('/')
  path
end