2 # Coquelicot: "one-click" file sharing with a focus on users' privacy.
3 # Copyright © 2010 potager.org <jardiniers@potager.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 $:.unshift File.join(File.dirname(__FILE__), '../lib')
23 def initialize(jyraphe_var)
30 puts "RewriteEngine on"
31 @redirects.each_pair do |jyraphe, coquelicot|
32 puts "RewriteRule ^file-#{jyraphe}$ #{coquelicot} [L,R=301]"
37 Dir.glob("#{@var}/links/*").each do |link_path|
38 link_name = File.basename(link_path)
39 one_time_only = link_name.slice(0, 1) == 'O'
40 File.open(link_path) do |link_file|
41 filename = link_file.readline.strip
42 mime_type = link_file.readline.strip
43 length = link_file.readline.strip.to_i
44 next if length > 10 * 1024 * 1024
45 file_key = link_file.readline.strip
46 if file_key.empty? then
47 random_pass = Coquelicot::gen_random_pass
49 expire_at = link_file.readline.strip.to_i
50 expire_at = [Time.now + Coquelicot.settings.maximum_expire,
51 expire_at].min if expire_at <= 0
53 coquelicot_link = File.open("#{@var}/files/#{filename}") do |src|
54 Coquelicot::depot.add_file(
55 file_key || random_pass,
56 { "Expire-at" => expire_at,
57 "One-time-only" => one_time_only,
58 "Filename" => filename,
59 "Content-Type" => mime_type
60 }) { src.eof? ? nil : src.read }
62 @redirects[link_name] = "#{coquelicot_link}"
63 @redirects[link_name] << "-#{random_pass}" if file_key.empty?
64 rescue Errno::ENOENT => ex
73 STDERR.puts "Usage: #{$0} </path/to/jyraphe/var> </path/to/coquelicot/depot>"
78 usage unless ARGV.length == 2
80 coquelicot_depot = ARGV[1]
82 unless File.directory? "#{jyraphe_var}/files" and
83 File.directory? "#{jyraphe_var}/links" then
84 STDERR.puts "#{jyraphe_var} is not a Jyraphe 'var' directory."
87 unless File.exists? "#{coquelicot_depot}/.links" then
88 STDERR.puts "#{coquelicot_depot} is not a Coquelicot depot."
92 Coquelicot::Application.set :depot_path, coquelicot_depot
93 JyrapheMigrator.new(jyraphe_var).process