Class: Gollum::File

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

Direct Known Subclasses

Page

Constant Summary collapse

!Gem.win_platform?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki, blob, path, version, try_on_disk = false) ⇒ File

Public: Initialize a file.

wiki - The Gollum::Wiki blob - The Gollum::Git::Blob path - The String path version - The String SHA or Gollum::Git::Commit version try_on_disk - If true, try to get an on disk reference for this file.

Returns a newly initialized Gollum::File.



84
85
86
87
88
89
90
# File 'lib/gollum-lib/file.rb', line 84

def initialize(wiki, blob, path, version, try_on_disk = false)
  @wiki         = wiki
  @blob         = blob
  @path         = self.class.canonical_path(path, blob.name)
  @version      = version.is_a?(Gollum::Git::Commit) ? version : @wiki.commit_for(version)
  get_disk_reference if try_on_disk
end

Instance Attribute Details

#on_diskObject

Public: Whether the file can be read from disk.



101
102
103
# File 'lib/gollum-lib/file.rb', line 101

def on_disk
  @on_disk
end

#pathObject (readonly)

Public: The path of the page within the repo.

Returns the String path.



95
96
97
# File 'lib/gollum-lib/file.rb', line 95

def path
  @path
end

#versionObject

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



98
99
100
# File 'lib/gollum-lib/file.rb', line 98

def version
  @version
end

Class Method Details

.canonical_path(*path, page_file_dir: nil) ⇒ Object

Get a canonical path to a file. Ensures that the result is always under page_file_dir (prevents path traversal), if set. Removes leading slashes.

path - One or more String path elements to join together. ‘nil` values are ignored. page_file_dir - kwarg String, default: nil



17
18
19
20
21
22
23
# File 'lib/gollum-lib/file.rb', line 17

def canonical_path(*path, page_file_dir: nil)
  prefix = Pathname.new('/') + page_file_dir.to_s
  rest = Pathname.new('/').join(*path.compact).cleanpath.to_s[1..-1]
  result = (prefix + rest).cleanpath.to_s[1..-1]
  result.sub!(/^\/+/, '') if Gem.win_platform? # On Windows, Pathname#cleanpath will leave double slashes at the start of a path, so replace all (not just the first) leading slashes
  result
end

.find(wiki, path, version, try_on_disk = false, global_match = false) ⇒ Object

Find a file in the given Gollum wiki.

wiki - The wiki. path - 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.

global_match - If true, find a File matching path’s filename, but not its directory (so anywhere in the repo)

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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gollum-lib/file.rb', line 52

def self.find(wiki, path, version, try_on_disk = false, global_match = false)
  query_path =  self.canonical_path(path, page_file_dir: wiki.page_file_dir)
  dir, filename = Pathname.new(query_path).split
  dir = dir.to_s

  if global_match && self.respond_to?(:global_find) # Only implemented for Gollum::Page
    return self.global_find(wiki, version, query_path, try_on_disk) 
  else
    begin
      root = wiki.commit_for(version)
      return nil unless root
      tree = dir == '.' ? root.tree : root.tree / dir
      return nil unless tree
      entry = tree.find_blob do |blob_name|
        path_compare(filename.to_s, blob_name, wiki.hyphened_tag_lookup, wiki.case_insensitive_tag_lookup)
      end
      entry ? self.new(wiki, entry, dir, version, try_on_disk) : nil
    rescue Gollum::Git::NoSuchShaFound
      nil
    end
  end
end

.path_compare(query, match_path, hyphened_tags, case_insensitive) ⇒ Object

For use with self.find: returns true if ‘query’ and ‘match_path’ match, strictly or taking account of the following parameters: hyphened_tags - If true, replace spaces in match_path with hyphens. case_insensitive - If true, compare query and match_path case-insensitively



28
29
30
31
32
33
34
35
36
37
# File 'lib/gollum-lib/file.rb', line 28

def path_compare(query, match_path, hyphened_tags, case_insensitive)
  if hyphened_tags
    final_query = query.gsub(' ', '-')
    final_match = match_path.gsub(' ', '-')
  else
    final_query = query
    final_match = match_path
  end
  final_match.send(case_insensitive ? :casecmp? : :==, final_query)
end

.protected_filesObject



167
168
169
# File 'lib/gollum-lib/file.rb', line 167

def self.protected_files
  ['custom.css', 'custom.js', '.redirects.gollum']
end

Instance Method Details

#escaped_url_pathObject

Public: The url_path, but URL encoded.

Returns the String url_path



129
130
131
# File 'lib/gollum-lib/file.rb', line 129

def escaped_url_path
  ERB::Util.url_encode(self.url_path).gsub('%2F', '/').force_encoding('utf-8')
end

#filenameObject Also known as: name

Public: The on-disk filename of the page including extension.

Returns the String name.



113
114
115
# File 'lib/gollum-lib/file.rb', line 113

def filename
  @blob && @blob.name
end

#mime_typeObject

Public: The String mime type of the file.



163
164
165
# File 'lib/gollum-lib/file.rb', line 163

def mime_type
  @blob && @blob.mime_type
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)


151
152
153
# File 'lib/gollum-lib/file.rb', line 151

def on_disk?
  !!@on_disk
end

#on_disk_pathObject

Public: The path to this file on disk

Returns nil if on_disk? is false.



158
159
160
# File 'lib/gollum-lib/file.rb', line 158

def on_disk_path
  @on_disk_path
end

#raw_dataObject

Public: The raw contents of the file.

Returns the String data.



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gollum-lib/file.rb', line 136

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

#shaObject

Public: The SHA hash identifying this page

Returns the String SHA.



106
107
108
# File 'lib/gollum-lib/file.rb', line 106

def sha
  @blob && @blob.id
end

#url_pathObject

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

Returns the String url_path



121
122
123
124
# File 'lib/gollum-lib/file.rb', line 121

def url_path
  # Chop off the page_file_dir and first slash if necessary
  @wiki.page_file_dir ? self.path[@wiki.page_file_dir.length+1..-1] : self.path
end