This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
forked from gollum/gollum
/
file.rb
77 lines (68 loc) · 1.86 KB
/
file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Gollum
class File
Wiki.file_class = self
# Public: Initialize a file.
#
# wiki - The Gollum::Wiki in question.
#
# Returns a newly initialized Gollum::File.
def initialize(wiki)
@wiki = wiki
@blob = nil
@path = nil
end
# Public: The on-disk filename of the file.
#
# Returns the String name.
def name
@blob && @blob.name
end
alias filename name
# Public: The raw contents of the page.
#
# Returns the String data.
def raw_data
@blob && @blob.data
end
# Public: The Grit::Commit version of the file.
attr_accessor :version
# Public: The String path of the file.
attr_reader :path
# Public: The String mime type of the file.
def mime_type
@blob.mime_type
end
# Populate the File with information from the Blob.
#
# blob - The Grit::Blob that contains the info.
# path - The String directory path of the file.
#
# Returns the populated Gollum::File.
def populate(blob, path=nil)
@blob = blob
@path = "#{path}/#{blob.name}"[1..-1]
self
end
#########################################################################
#
# Internal Methods
#
#########################################################################
# Find a file in the given Gollum repo.
#
# name - The full String path.
# version - The String version ID to find.
#
# Returns a Gollum::File or nil if the file could not be found.
def find(name, version)
checked = name.downcase
map = @wiki.tree_map_for(version)
if entry = map.detect { |entry| entry.path.downcase == checked }
@path = name
@blob = entry.blob(@wiki.repo)
@version = version.is_a?(Grit::Commit) ? version : @wiki.commit_for(version)
self
end
end
end
end