From 84920c358a698fefc4227e856782f44a7d30aa7e Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 1 Nov 2012 23:36:36 +0600 Subject: [PATCH] [refs file-store-4] upload to file-store --- Gemfile | 2 ++ Gemfile.lock | 3 +++ bin/calc_sha | 2 +- bin/calc_sha_help | 23 ----------------------- bin/file-store.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 24 deletions(-) delete mode 100755 bin/calc_sha_help create mode 100755 bin/file-store.rb diff --git a/Gemfile b/Gemfile index 60869b4bb..b17e44123 100644 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,8 @@ gem 'jquery-rails', '~> 2.0.2' gem 'ruby-haml-js', '~> 0.0.3' gem 'rails-backbone', '~> 0.7.2' +gem 'rest-client', '~> 1.6.6' + group :assets do gem 'sass-rails', '~> 3.2.5' gem 'coffee-rails', '~> 3.2.2' diff --git a/Gemfile.lock b/Gemfile.lock index 896f7a8ab..120e32ba3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -269,6 +269,8 @@ GEM uuid (~> 2.3) resque_mailer (2.1.0) actionmailer (~> 3.0) + rest-client (1.6.7) + mime-types (>= 1.16) rr (1.0.4) rspec (2.11.0) rspec-core (~> 2.11.0) @@ -404,6 +406,7 @@ DEPENDENCIES resque (~> 1.21.0) resque-status (~> 0.3.3) resque_mailer (~> 2.1.0) + rest-client (~> 1.6.6) rr (~> 1.0.4) rspec-rails (~> 2.11.0) ruby-haml-js (~> 0.0.3) diff --git a/bin/calc_sha b/bin/calc_sha index 7e19aa8a9..f0eaaf396 100755 --- a/bin/calc_sha +++ b/bin/calc_sha @@ -2,5 +2,5 @@ pwd=`pwd` command="$(pwd)/calc_sha_help $2" cd $1 -git filter-branch --tree-filter "$pwd/bin/calc_sha_help $2" --prune-empty --tag-name-filter cat -- --all +git filter-branch --tree-filter "$pwd/bin/file-store" --prune-empty --tag-name-filter cat -- --all cd .. diff --git a/bin/calc_sha_help b/bin/calc_sha_help deleted file mode 100755 index d7aec16e2..000000000 --- a/bin/calc_sha_help +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -find . -regex ".*\.\(tar\.bz2\|tar\.gz\|bz2\|rar\|gz\|tar\|tbz2\|tgz\|zip\|Z\|7z\)" -size +2M | sort | while read file; -do - res=`sha1sum "$file"` - sha=( $res ) - if [ -d "$1/$sha" ] - then - filename=`ls "$1/$sha"` - else - filename=${file:2} - mkdir -p $1/$sha - cp -n "$file" "$1/$sha/" - fi - - echo " \"${filename}\": $sha" >> abf_temp_temp.yml - rm -rf "$file" -done -if [ -e abf_temp_temp.yml ] -then - echo 'sources:' > abf.yml - cat abf_temp_temp.yml >> abf.yml - rm -rf abf_temp_temp.yml -fi diff --git a/bin/file-store.rb b/bin/file-store.rb new file mode 100755 index 000000000..f41502238 --- /dev/null +++ b/bin/file-store.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby +require 'json' +require 'rest-client' + +abf_yml, new_sources = 'abf.yml', [] +old_sources = if File.exist? abf_yml + File.read(abf_yml).split("\n").reject {|line| line =~ /sources/} + else + [] + end +MAX_SIZE = 2 * 1024 * 1024 # 2.megabytes +url = 'http://file-store.rosalinux.ru/api/v1/file_stores.json' +#url = 'http://localhost:3001/api/v1/file_stores.json' +user, pass = ARGF.argv[0] || 'CENSORED', ARGF.argv[1] || 'CENSORED' # FIXME +rclient = RestClient::Resource.new(url, user, pass) + +Dir.glob("*.{tar\.bz2,tar\.gz,bz2,rar,gz,tar,tbz2,tgz,zip,Z,7z}").uniq.sort.each do |file| + begin + puts "Work with file \"#{file}\"" + next if File.size(file) < MAX_SIZE + + sha1 = Digest::SHA1.file(file).hexdigest + resp = JSON(RestClient.get url, :params => {:hash => sha1}) + if resp[0].respond_to?('[]') && resp[0]['file_name'] && resp[0]['sha1_hash'] + # file already exists at file-store + new_sources << " \"#{file}\": #{sha1}" + FileUtils.rm_rf file + elsif resp == [] + # try to put file at file-store + resp = JSON(rclient.post :file_store => {:file => File.new(file, 'rb')}) + unless resp['sha1_hash'].nil? + new_sources << " \"#{file}\" #{sha1}" + FileUtils.rm_rf file + end + else + raise "Response unknown!\n #{resp}" + end + + #rescue => e + # e.response + end +end + +File.open(abf_yml, 'w') do |abf| + abf.puts 'sources:' + (old_sources | new_sources).sort.each {|line| abf.puts line} +end