From 59feb099284801aff84246850c27420610f82e4b Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Wed, 27 May 2015 16:06:56 -0700 Subject: [PATCH] integrate IPFS using remote ssh for now, views --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 19 +++++++++++++++++++ app/site.rb | 11 +++++++++++ models/archive.rb | 4 ++++ models/site.rb | 23 +++++++++++++++++++---- views/site.erb | 7 +++++++ views/site/archives.erb | 30 ++++++++++++++++++++++++++++++ 8 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 views/site/archives.erb diff --git a/.gitignore b/.gitignore index c44ce044..0abd85ad 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ files/sslsites.zip .vagrant public/banned_sites public/deleted_sites +tests/stat_logs/* diff --git a/Gemfile b/Gemfile index 5a75ade3..28dc5451 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem 'rack-cache' gem 'rest-client' gem 'geoip' gem 'io-extra', require: 'io/extra' +gem 'rye' platform :mri, :rbx do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index 6e6aa062..be313793 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,6 +9,8 @@ GEM tzinfo (~> 1.1) addressable (2.3.7) ago (0.1.5) + annoy (0.5.6) + highline (>= 1.5.0) ansi (1.4.3) autoparse (0.3.3) addressable (>= 2.3.1) @@ -50,6 +52,7 @@ GEM docile (1.1.3) domain_name (0.5.23) unf (>= 0.0.5, < 1.0.0) + drydock (0.6.9) erubis (2.7.0) extlib (0.9.16) fabrication (2.11.0) @@ -75,6 +78,7 @@ GEM signet (>= 0.5.0) uuidtools (>= 2.1.0) hashie (2.0.5) + highline (1.7.2) hiredis (0.5.0) http-cookie (1.0.2) domain_name (~> 0.5) @@ -110,6 +114,9 @@ GEM metaclass (~> 0.0.1) multi_json (1.11.0) multipart-post (2.0.0) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (2.9.2) netrc (0.10.3) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) @@ -160,6 +167,13 @@ GEM netrc (~> 0.7) retriable (1.4.1) rmagick (2.15.0) + rye (0.9.13) + annoy + docile (>= 1.0.1) + highline (>= 1.5.1) + net-scp (>= 1.0.2) + net-ssh (>= 2.0.13) + sysinfo (>= 0.8.1) safe_yaml (1.0.4) sass (3.3.8) screencap (0.1.1) @@ -198,6 +212,7 @@ GEM sinatra-xsendfile (0.4.2) sinatra (>= 0.9.1) slop (3.5.0) + storable (0.8.9) stripe (1.15.0) json (~> 1.8.1) mime-types (>= 1.25, < 3.0) @@ -206,6 +221,9 @@ GEM dante (>= 0.2.0) jimson-temp stripe (>= 1.15.0) + sysinfo (0.8.1) + drydock + storable thread (0.1.4) thread_safe (0.3.4) tilt (1.4.1) @@ -273,6 +291,7 @@ DEPENDENCIES rest-client rmagick ruby-debug + rye sass screencap scrypt diff --git a/app/site.rb b/app/site.rb index 5600f8fc..d673ae4a 100644 --- a/app/site.rb +++ b/app/site.rb @@ -32,6 +32,17 @@ get '/site/:username/?' do |username| erb :'site', locals: {site: site, is_current_site: site == current_site} end +get '/site/:username/archives' do + require_login + @site = Site[username: params[:username]] + not_found if @site.nil? + redirect request.referrer unless current_site.id == @site.id + + @archives = @site.archives_dataset.limit(300).order(:updated_at.desc).all + + erb :'site/archives' +end + get '/site/:username/stats' do @site = Site[username: params[:username]] not_found if @site.nil? diff --git a/models/archive.rb b/models/archive.rb index 0df7e093..53731056 100644 --- a/models/archive.rb +++ b/models/archive.rb @@ -2,4 +2,8 @@ class Archive < Sequel::Model many_to_one :site set_primary_key [:site_id, :ipfs_hash] unrestrict_primary_key + + def url + "https://#{ipfs_hash}.ipfs.neocities.org" + end end diff --git a/models/site.rb b/models/site.rb index faf81cff..aec94768 100644 --- a/models/site.rb +++ b/models/site.rb @@ -558,10 +558,19 @@ class Site < Sequel::Model end def add_to_ipfs - line = Cocaine::CommandLine.new('ipfs', 'add -r :path') - response = line.run path: files_path - ipfs_hash = response.split("\n").last.split(' ')[1] - ipfs_hash + # Not ideal. An SoA version is in progress. + if $config['ipfs_ssh_host'] && $config['ipfs_ssh_user'] + Rye::Cmd.add_command :ipfs, nil, 'add', :r + rbox = Rye::Box.new $config['ipfs_ssh_host'], :user => $config['ipfs_ssh_user'] + response = rbox.ipfs "sites/#{self.username.gsub(/\/|\.\./, '')}" + output_array = response + else + line = Cocaine::CommandLine.new('ipfs', 'add -r :path') + response = line.run path: files_path + output_array = response.to_s.split("\n") + end + + output_array.last.split(' ')[1] end def archive! @@ -570,6 +579,8 @@ class Site < Sequel::Model #else #end + ipfs_hash = add_to_ipfs + archive = archives_dataset.where(ipfs_hash: ipfs_hash).first if archive archive.updated_at = Time.now @@ -579,6 +590,10 @@ class Site < Sequel::Model end end + def latest_archive + @latest_archive ||= archives_dataset.order(:updated_at.desc).first + end + def is_directory?(path) File.directory? files_path(path) end diff --git a/views/site.erb b/views/site.erb index f59d00dd..d4dc6108 100644 --- a/views/site.erb +++ b/views/site.erb @@ -21,6 +21,9 @@

<%= site.title %> <% if site.supporter? %> <% end %>

<%= site.host %>

+ <% if site.latest_archive %> +

IPFS Link(what is this?)

+ <% end %>
<%= site.views.format_large_number %> view<%= site.views == 1 ? '' : 's' %>
<% follows_count = site.follows_dataset.count %> @@ -33,6 +36,10 @@ Edit Site <% end %> + <% if current_site && current_site.id == site.id && site.latest_archive %> + Archives + <% end %> + <% if current_site && current_site != site %> <% is_following = current_site.is_following?(site) %> diff --git a/views/site/archives.erb b/views/site/archives.erb new file mode 100644 index 00000000..a42e91f4 --- /dev/null +++ b/views/site/archives.erb @@ -0,0 +1,30 @@ +
+
+

Permanent Web Archives

+
+
+ +
+ +