From e75ee43e9666b74538e4ef4c1f9984325f038f28 Mon Sep 17 00:00:00 2001 From: Elias Gabriel Amaral da Silva Date: Thu, 4 Nov 2010 15:53:39 -0300 Subject: [PATCH] Thumbnails working!! --- src/add.ml | 3 ++- src/db.ml | 17 ++++++++++++++++- src/dir.ml | 15 +++++++++++++++ src/image.ml | 5 +++-- web/db.rb | 14 +++++++++++--- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/add.ml b/src/add.ml index f0a4442..608b990 100644 --- a/src/add.ml +++ b/src/add.ml @@ -14,7 +14,8 @@ let link_file db file = let add_file db file = let file_id' = link_file db file in - insert_file_rel db file_id' file + let thumbs = write_thumbnails file in + insert_file_rel db file_id' thumbs file let add db dir fs = let bag_id = insert_bag db dir in diff --git a/src/db.ml b/src/db.ml index 6aa0f02..f123dcc 100644 --- a/src/db.ml +++ b/src/db.ml @@ -64,12 +64,27 @@ let insert_file db file = returning file_id" in read_id l +let insert_thumb db file_id n w h max_w max_h = + let image_id = insert_image db (w, h, 0l) in + PGSQL(db) "insert into thumbnail + (file_id, image_id, max_width, max_height, scaled, repo_path) + values + ($file_id, $image_id, $max_w, $max_h, true, $n)" + +let insert_thumbs db file_id thumbs = + let f = function + None -> () + | Some (n, w, h, max_w, max_h) -> + insert_thumb db file_id n w h max_w max_h in + List.iter f thumbs + let fid_if_exists db file = function Some a -> a | None -> insert_file db file -let insert_file_rel db file_id' file = +let insert_file_rel db file_id' thumbs file = let file_id = fid_if_exists db file file_id' in + insert_thumbs db file_id thumbs; insert_bag_file db file_id file let connect () = PGOCaml.connect () diff --git a/src/dir.ml b/src/dir.ml index a19b451..2701711 100644 --- a/src/dir.ml +++ b/src/dir.ml @@ -22,6 +22,7 @@ struct prev_path : string; bag_id : int64; size : int64; + ext: string } end @@ -60,6 +61,19 @@ let name md5 size ext is_thumb info = | _ -> sprintf "%s-%Ld.%s" md5 size ext +let write_thumbnail { md5; size; ext; prev_path; dir } max_w max_h = + let f w h = concat dir (name md5 size ext true (Some(w, h, true))) in + match Image.r max_w max_h prev_path f with + None -> None + | Some (n, w', h') -> Some (n, w', h', + Int32.of_int max_w, + Int32.of_int max_h) + +let thumbnails = [(840, 630); (600, 450)] + +let write_thumbnails file = + List.map (fun (w, h) -> write_thumbnail file w h) thumbnails + let new_file bag_id origin pos = let md5 = Digest.to_hex (Digest.file origin) in let size = Int64.of_int (size_of origin) in @@ -78,6 +92,7 @@ let new_file bag_id origin pos = pos = Int64.of_int pos; md5; size; + ext; mime; magic; prev_name = basename origin; diff --git a/src/image.ml b/src/image.ml index 54a5ffe..8e79327 100644 --- a/src/image.ml +++ b/src/image.ml @@ -29,8 +29,9 @@ let resize' maxw maxh img f = let img' = Fun.resize ~width:w ~height:h ~filter:Lanczos ~blur:1.0 () img in Imper.set_compression_quality img' 85; let w', h' = Int32.of_int w, Int32.of_int h in - write_image img' (f w' h'); - (w', h') + let n = f w' h' in + write_image img' n; + (n, w', h') (* Example: r 300 300 "/q/a/01.jpg" (sprintf "/q/ahm-%ldx%ld.jpg") *) diff --git a/web/db.rb b/web/db.rb index 294d2b2..e4bb573 100644 --- a/web/db.rb +++ b/web/db.rb @@ -12,14 +12,22 @@ def list_bags_by_page(gap, n, &blk) blk.call(r) end end - def list_files_of_bag(id, &blk) + def list_files_of_bag_(id, &blk) $db.fetch("select file.repo_path from file natural join bag_file where file.image_id is not null - and bag_file.bag_id = 1 + and bag_file.bag_id = ? order by bag_file.pos", id) { |r| blk.call(r) } end - + def list_files_of_bag(id, &blk) + $db.fetch("select thumbnail.repo_path from thumbnail, (file + natural join bag_file) + where thumbnail.file_id = file.file_id + and bag_file.bag_id = ? + and thumbnail.max_width = 840 + and thumbnail.max_height = 630 + order by bag_file.pos", id) { |r| blk.call(r) } + end # ugly def list_empty_bags(&blk)