Skip to content

Commit

Permalink
Thumbnails working!!
Browse files Browse the repository at this point in the history
  • Loading branch information
dlight committed Nov 4, 2010
1 parent f64e3ae commit e75ee43
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/add.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/db.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
15 changes: 15 additions & 0 deletions src/dir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct
prev_path : string;
bag_id : int64;
size : int64;
ext: string
}
end

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/image.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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") *)

Expand Down
14 changes: 11 additions & 3 deletions web/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e75ee43

Please sign in to comment.