Skip to content

Commit

Permalink
New endpoint for images created
Browse files Browse the repository at this point in the history
  • Loading branch information
LightGuard committed Nov 1, 2013
1 parent b7b7216 commit e3f694d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
27 changes: 16 additions & 11 deletions helpers/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def clone_repo
end
end

def all_files(allows = [])
def all_files(dir = '', allows = [])
@logger.info "Finding all files, additional allows #{allows}"
default_allows = [%r!(.ad)|(.adoc)|(.adoc)|(.jpg)|(.jpeg)|(.png)|(.gif)!]
default_allows << allows.join unless allows.empty?
Expand All @@ -108,15 +108,15 @@ def all_files(allows = [])
default_ignores = [%r!(.gitignore$)|(.git$)|(_site$)|(.awestruct$)|(.awestruct_ignore$)|(_config$)|(_ext$)|(.git$)|(.travis.yml$)|(_tmp$)|(.sass-cache$)!]
files = []

if File.exists? base_repository_path
Find.find(base_repository_path) do |path|
if File.exists?(File.join(base_repository_path, dir))
Find.find(File.join(base_repository_path, dir)) do |path|
if Regexp.union(default_ignores).match(path.to_s)
Find.prune
end

if regexp_ignores.match(path.to_s) || File.directory?(path)
if File.basename(path.to_s) != @name
files << file_info(path)
files << file_info(path, dir)
end
end
end
Expand Down Expand Up @@ -226,24 +226,29 @@ def pull_request(title, body)
pull_request_result['html_url']
end

def file_content(file, binary = false)
def file_content(file)
@logger.info "reading contents of file #{file.to_s}"
if binary
File.open(File.join(base_repository_path, file), 'rb').read

file_path = File.join(base_repository_path, file)

return '' unless File.exists? file_path

if `file --mime-encoding -b #{file_path}` =~ /binary/
"data:#{`file --mime-type -b #{file_path}`.strip};base64,#{Base64.encode64(File.open(file_path, 'rb').read)}"
else
File.open(File.join(base_repository_path, file), 'r').read
File.open(file_path, 'r').read
end
end

def file_info(path)
def file_info(path, dir = '')
path = Pathname.new(path)
path = Pathname.new(File.join(base_repository_path, path)) unless File.exists? path
path = Pathname.new(File.join(base_repository_path, dir, path.basename)) unless File.exists? path
{ :location => path.basename.to_s, :directory => path.directory?,
:path_to_root => path.relative_path_from(Pathname.new base_repository_path).dirname.to_s }
end

def log(count = 30)
@logger.info "retreiving the last #{count} log entries"
@logger.info "retrieving the last #{count} log entries"
@git_repo.log count
end

Expand Down
14 changes: 14 additions & 0 deletions public_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ class PublicApp < Sinatra::Base
[200, JSON.dump(return_links)]
end

get '/repo/:repo_name/images' do |repo_name|
repo = create_repo(repo_name)
files = repo.all_files('images')

json_return = {}
files.each do |f|
unless f[:directory]
json_return[f[:location]] = { :content => repo.file_content(File.join('images', f[:location]))}
end
end

[200, JSON.dump(json_return)]
end

get '/repo/:repo_name/*' do |repo_name, path|
repo = create_repo(repo_name)
json_return = { :content => repo.file_content(path), :links => links_for_file(repo.file_info(path), repo_name) }
Expand Down

0 comments on commit e3f694d

Please sign in to comment.