Amélioration du panneau de configuration, et préparation d'un assistant de premier...
[bilio:bilio.git] / scan.rb
1 #!/usr/bin/env ruby
2 # coding: utf-8
3
4 #
5 # Need of scanimage (sane), and convert (imagemagik) installed on system
6 #
7
8 class Scan
9         
10         def initialize conf
11                 @conf = conf.conf
12                 @chemin_documents = @conf["chemin_documents"] if @conf["chemin_documents"]
13                 @scanner = @conf["scanner"] if @conf["scanner"]
14                 @resolution = @conf["resolution"].to_i if @conf["resolution"]
15         end
16
17         def scanner mode
18                 p `/usr/bin/scanimage -d "#{@scanner}" --resolution #{@resolution}dpi -l 0mm -t 0mm -x 210mm -y 297mm --mode #{mode} > "scan.pnm"`
19         end
20         
21         def creer_dossier id, tiers
22                 if File.directory?(@chemin_documents)
23                         chemin = "#{@chemin_documents}/#{(tiers ? "tiers" : "documents")}/"
24                         Dir.mkdir(chemin) unless File.directory?(chemin)
25                         chemin += "#{id}/"
26                         Dir.mkdir(chemin) unless File.directory?(chemin)
27                         return chemin
28                 else
29                         p "Erreur du chemin de base"
30                         return nil
31                 end
32         end
33
34         def convertir chemin
35                 p `pnmtojpeg --quality=50 scan.pnm > scan.jpg`
36                 p `convert -depth #{@resolution} -resize 1000x1500 scan.jpg #{chemin}`
37                 miniature = "#{File.dirname(chemin)}/#{File.basename(chemin, File.extname(chemin))}_miniature.jpg"
38                 p `convert scan.jpg -resize 80x80 #{miniature}`
39         end
40
41         def nettoyer
42                 p `rm scan.pnm scan.jpg`
43         end
44         
45         def renommer nom, chemin, ext
46                 nom_def = nom
47                 i=0
48                 while File.exist?("#{chemin}#{nom_def}#{ext}") do
49                         i += 1
50                         nom_def = nom + i.to_s
51                 end
52                 nom_def
53         end
54
55         def acquisition nom, id, tiers=true, ext=".pdf", mode="Gray"
56                 chemin = creer_dossier id, tiers
57                 if chemin
58                         nom_def = renommer nom, chemin, ext
59                         scanner mode
60                         convertir "#{chemin}#{nom_def}#{ext}"
61                         nettoyer
62                         if tiers
63                                 ged = Gedtiers.new
64                                 ged.tiers_id = id
65                         else
66                                 ged = Geddocument.new
67                                 ged.document_id = id
68                         end
69                         ged.fichier = nom_def+ext
70                         ged.save
71                 end
72         end
73         
74 end