From e3f694dae0cbf00509865aa0f06204be79657284 Mon Sep 17 00:00:00 2001 From: LightGuard Date: Fri, 1 Nov 2013 14:28:07 -0600 Subject: [PATCH] New endpoint for images created --- helpers/repository.rb | 27 ++++++++++++++++----------- public_app.rb | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/helpers/repository.rb b/helpers/repository.rb index 1743a11..4c3befe 100644 --- a/helpers/repository.rb +++ b/helpers/repository.rb @@ -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? @@ -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 @@ -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 diff --git a/public_app.rb b/public_app.rb index beaaad1..68349ff 100644 --- a/public_app.rb +++ b/public_app.rb @@ -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) }