/* Cataract - Static web photo gallery generator * Copyright (C) 2008 Tomas Bzatek * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include "setup.h" #include "items.h" #include "jpeg-utils.h" #include "gallery-utils.h" #include "generators-replace-table.h" #define DEFAULT_DATA_DIR_MODE S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH /* * generate_image: auxiliary function for write_html_album * - img_src and thumb should be freed afterwards */ void generate_image (TGallerySetup *setup, TAlbum *items, TIndexItem *item, unsigned int item_index, const char *dst, unsigned long *img_w, unsigned long *img_h, char **img_src, char **thumb) { unsigned long new_w, new_h; unsigned long thumb_w, thumb_h; char *thumb_dst; char *big_dst; char *big_src; char *orig_dst; char *img_src_full; char *thumb_src_full; char *s1; int bigq; *img_src = NULL; *thumb = NULL; *img_w = 0; *img_h = 0; thumb_src_full = NULL; img_src_full = NULL; if (items->type == GALLERY_TYPE_INDEX) { if (item->thumbnail == NULL || strlen (item->thumbnail) == 0) return; img_src_full = g_strconcat (items->base_dir, "/", item->thumbnail, NULL); thumb_src_full = g_strconcat (items->base_dir, "/", item->thumbnail, NULL); *img_src = g_path_get_basename (item->thumbnail); s1 = g_path_get_basename (item->thumbnail); *thumb = g_strdup_printf ("thn_%.3d_%s", item_index, s1); g_free (s1); } else if (items->type == GALLERY_TYPE_ALBUM) { *img_src = (item->path == NULL && item->preview) ? item->preview : item->path; *thumb = (item->thumbnail) ? item->thumbnail : *img_src; img_src_full = g_strconcat (items->base_dir, "/", *img_src, NULL); thumb_src_full = g_strconcat (items->base_dir, "/", *thumb, NULL); *img_src = g_path_get_basename (*img_src); s1 = g_path_get_basename (*thumb); *thumb = g_strdup_printf ("thn_%.3d_%s", item_index, s1); g_free (s1); } get_image_sizes (img_src_full, img_w, img_h); if ((img_w > 0) && (img_h > 0)) { new_w = *img_w; new_h = *img_h; /* Generate thumbnail */ g_assert (thumb_src_full != NULL); get_image_sizes (thumb_src_full, &thumb_w, &thumb_h); s1 = g_path_get_dirname (dst); thumb_dst = g_strconcat (s1, "/", THUMBNAIL_DIR, "/", *thumb, NULL); g_free (s1); if (setup->verbose) printf (" Generating thumbnail of '%s' ...", *thumb); if ((thumb_w > 0) && (thumb_h > 0)) { if ((thumb_w / thumb_h) >= 1) calculate_sizes (setup->thumbnail_landscape_width, setup->thumbnail_landscape_height, &thumb_w, &thumb_h); else calculate_sizes (setup->thumbnail_portrait_width, setup->thumbnail_portrait_height, &thumb_w, &thumb_h); if (! resize_image (thumb_src_full, thumb_dst, thumb_w, thumb_h, setup->thumbnail_quality, TRUE)) fprintf (stderr, "write_html_index: error resizing thumbnail %s\n", thumb_src_full); else if (setup->verbose) printf (" done.\n"); g_free (thumb_dst); } else printf (" failed!\n"); /* Generate/copy preview and original image */ if (items->type == GALLERY_TYPE_ALBUM) { s1 = g_path_get_dirname (dst); big_dst = g_strconcat (s1, "/", IMG_BIG_DIR, "/", *img_src, NULL); g_free (s1); if (item->preview == NULL) { /* No preview image supplied, generate it from original */ bigq = setup->preview_quality; if ((items->quality > 0) && (items->quality <= 100)) bigq = items->quality; if ((item->quality > 0) && (item->quality <= 100)) bigq = item->quality; new_w = *img_w; new_h = *img_h; if (setup->verbose) printf (" Generating preview of '%s' ...", *img_src); if ((item->width > 0) && (item->height > 0)) { calculate_sizes (item->width, item->height, &new_w, &new_h); } else { if ((*img_w / *img_h) >= 1) { if ((items->landscape_width > 0) && (items->landscape_height > 0)) calculate_sizes (items->landscape_width, items->landscape_height, &new_w, &new_h); else calculate_sizes (setup->preview_landscape_width, setup->preview_landscape_height, &new_w, &new_h); } else { if ((items->portrait_width > 0) && (items->portrait_height > 0)) calculate_sizes (items->portrait_width, items->portrait_height, &new_w, &new_h); else calculate_sizes (setup->preview_portrait_width, setup->preview_portrait_height, &new_w, &new_h); } } g_assert (img_src_full != NULL); if (! resize_image (img_src_full, big_dst, new_w, new_h, bigq, FALSE)) fprintf (stderr, "write_html_index: error resizing big image %s\n", img_src_full); else if (setup->verbose) printf (" done.\n"); } else { /* Copy the preview (big) image provided */ big_src = g_strconcat (items->base_dir, "/", item->preview, NULL); if (setup->verbose) printf (" Copying preview image '%s' ...", *img_src); if (! copy_file (big_src, big_dst)) fprintf (stderr, "write_html_index: error copying preview image %s\n", big_src); else if (setup->verbose) printf (" done.\n"); g_free (big_src); } modify_exif (big_dst, setup->erase_exif_thumbnail, setup->add_copyright); g_free (big_dst); if (item->force_fullsize || (items->fullsize && ! item->force_nofullsize) || (! item->force_nofullsize && ! items->nofullsize && ! setup->nofullsize)) { s1 = g_path_get_dirname(dst); orig_dst = g_strconcat (s1, "/", IMG_ORIG_DIR, "/", *img_src, NULL); g_free (s1); if (setup->verbose) printf (" Copying original image '%s' ...", *img_src); if (! copy_file(img_src_full, orig_dst)) fprintf (stderr, "write_html_index: error copying original image %s\n", img_src_full); else if (setup->verbose) printf(" done.\n"); modify_exif (orig_dst, setup->erase_exif_thumbnail, setup->add_copyright); g_free (orig_dst); } } } if (img_src_full) g_free (img_src_full); if (thumb_src_full) g_free (thumb_src_full); } /* * write_html_album: process album and index template files * * template_src = template file of the album/index * dst = save generated file as * items = array of items in the album/index * */ gboolean write_html_album (TGallerySetup *setup, const char *template_src, const char *dst, TAlbum *items) { #define BUFFER_SIZE 65536 FILE *fin; FILE *fout; char *buffer; char *buf_img_list_landscape; char *buf_img_list_portrait; char *buf_img_separator; char *buf_go_up_string; gboolean in_img_list; gboolean in_img_list_landscape; gboolean in_img_list_portrait; gboolean in_img_separator; gboolean in_go_up_string; char *b; char *s1, *s2, *s3, *s4, *s5; TAlbum *parent; TIndexItem *item; TIndexItem *tmp_item; int level, old_parent_item_index; gboolean res, bb; int i; unsigned long img_w, img_h; char *img_src; char *thumb; unsigned int real_total_items; ReplaceTable *global_replace_table; ReplaceTable *local_replace_table; fin = fopen (template_src, "r"); if (fin == NULL) { fprintf (stderr, "write_html_index: error reading file \"%s\": %s\n", template_src, strerror (errno)); return FALSE; } fout = fopen (dst, "w"); if (fout == NULL) { fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); fclose (fin); return FALSE; } buffer = malloc (BUFFER_SIZE); buf_img_list_landscape = malloc (BUFFER_SIZE); buf_img_list_portrait = malloc (BUFFER_SIZE); buf_img_separator = malloc (BUFFER_SIZE); buf_go_up_string = malloc (BUFFER_SIZE); in_img_list = FALSE; in_img_list_landscape = FALSE; in_img_list_portrait = FALSE; in_img_separator = FALSE; in_go_up_string = FALSE; res = TRUE; global_replace_table = replace_table_new (); /* Get number of real pictures in the list */ real_total_items = 0; for (i = 0; i < items->items->len; i++) { tmp_item = g_ptr_array_index (items->items, i); if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE) real_total_items++; } replace_table_add_key_int (global_replace_table, "TOTAL_ITEMS", real_total_items); /* Page title */ if (items->parent_index == NULL || setup->site_title == NULL) s1 = g_strdup (setup->site_title ? setup->site_title : items->ID); else s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title); replace_table_add_key (global_replace_table, "PAGE_TITLE", s1); g_free (s1); /* Simple placeholders */ replace_table_add_key (global_replace_table, "ID", items->ID); replace_table_add_key (global_replace_table, "TITLE", items->title); replace_table_add_key (global_replace_table, "DESCRIPTION", items->desc); replace_table_add_key (global_replace_table, "FOOTNOTE", items->footnote); replace_table_add_key (global_replace_table, "FOOTER", setup->footer); /* Navigation bar (NOTE: 'int level' is used below + favicon) */ s1 = g_strdup (items->ID); old_parent_item_index = items->parent_item_index + 1; parent = items->parent_index; level = 1; if (parent) { tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) level += count_dir_levels (tmp_item->path) - 1; } /* Go Up string */ s3 = make_string ("../", level); s2 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); replace_table_add_key_printf (global_replace_table, "GO_UP_LINK", "%sindex.html%s", s3, s2); g_free (s2); free (s3); while (parent) { s3 = make_string ("../", level); s4 = g_strdup (parent->ID); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); free (s3); g_free (s1); g_free (s4); g_free (s5); s1 = s2; old_parent_item_index = parent->parent_item_index + 1; parent = parent->parent_index; level++; if (parent) { tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) level += count_dir_levels (tmp_item->path) - 1; } } replace_table_add_key (global_replace_table, "NAV_BAR", s1); g_free (s1); /* Supportfiles path */ s1 = make_string ("../", level - 1); replace_table_add_key (global_replace_table, "TEMPLATES_PATH", setup->support_files_use_common_root ? s1 : ""); free (s1); /* META tags */ s1 = g_strdup_printf ("\t\n", VERSION); if (setup->meta_author || items->meta_author) { s3 = g_strdup (items->meta_author ? items->meta_author : setup->meta_author); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_description || items->meta_description) { s3 = g_strdup (items->meta_description ? items->meta_description : setup->meta_description); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_keywords || items->meta_keywords) { s3 = g_strdup (items->meta_keywords ? items->meta_keywords : setup->meta_keywords); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if (setup->favicon_file && strlen (setup->favicon_file) > 0) { s3 = make_string ("../", level - 1); if (setup->favicon_type) s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); else s2 = g_strdup_printf ("%s\t\n", s1, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); g_free (s1); free (s3); s1 = s2; } replace_table_add_key (global_replace_table, "CGG_META_TAGS", s1); g_free (s1); /* Read through the template and replace placeholders with real data */ while (! feof (fin)) { memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; /* Block placeholders */ if (in_img_list && (strstr (buffer, ""))) { in_img_list_landscape = TRUE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_landscape = FALSE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_portrait = TRUE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_portrait = FALSE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_separator = TRUE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_separator = FALSE; continue; } if (in_img_list && in_img_list_landscape) { buf_img_list_landscape = strncat (buf_img_list_landscape, buffer, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2); continue; } if (in_img_list && in_img_list_portrait) { buf_img_list_portrait = strncat (buf_img_list_portrait, buffer, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2); continue; } if (in_img_list && in_img_separator) { buf_img_separator = strncat (buf_img_separator, buffer, BUFFER_SIZE - strlen (buf_img_separator) - 2); continue; } if (strstr (buffer, "")) { memset (buf_go_up_string, 0, BUFFER_SIZE); in_go_up_string = TRUE; continue; } if (in_go_up_string && strstr (buffer, "")) { in_go_up_string = FALSE; if (! items->parent_index || ! setup->show_go_up) continue; } if (in_go_up_string) { buf_go_up_string = strncat (buf_go_up_string, buffer, BUFFER_SIZE - strlen (buf_go_up_string) - 2); continue; } /* Image list, nested placeholders */ if (strstr (buffer, "")) { memset (buf_img_list_landscape, 0, BUFFER_SIZE); memset (buf_img_list_portrait, 0, BUFFER_SIZE); memset (buf_img_separator, 0, BUFFER_SIZE); in_img_list = TRUE; in_img_list_landscape = FALSE; in_img_list_portrait = FALSE; in_img_separator = FALSE; continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list = FALSE; in_img_list_landscape = FALSE; in_img_list_portrait = FALSE; /* Now we have all block placeholders read, generate the items: */ for (i = 0; i < items->items->len; i++) { item = g_ptr_array_index (items->items, i); if (item == NULL) { fprintf (stderr, "write_html_index: error getting item %d\n", i); continue; } /* Generate the images (preview, original, thumbnail) */ local_replace_table = replace_table_new (); img_w = 0; img_h = 0; s1 = NULL; switch (item->type) { case INDEX_ITEM_TYPE_PICTURE: img_src = NULL; thumb = NULL; generate_image (setup, items, item, i, dst, &img_w, &img_h, &img_src, &thumb); /* Skip HTML code generation if it's a hidden item */ if (! item->hidden) { if (img_w == 0 || img_h == 0 || (img_w / img_h) >= 1) s1 = strdup (buf_img_list_landscape); else s1 = strdup (buf_img_list_portrait); replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/index.html", item->path); replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s.html", img_src); replace_table_add_key (local_replace_table, "IMG_TITLE", item->title); replace_table_add_key (local_replace_table, "IMG_DESCRIPTION", item->title_description); replace_table_add_key_printf (local_replace_table, "IMG_LIST_ID", "i%d", i + 1); replace_table_add_key_printf (local_replace_table, "IMG_THUMBNAIL", "%s/%s", THUMBNAIL_DIR, thumb); replace_table_add_key (local_replace_table, "IMG_FILENAME", img_src); if (items->type == GALLERY_TYPE_INDEX) { s3 = g_strconcat (items->base_dir, "/", item->path, "/index.xml", NULL); replace_table_add_key_int (local_replace_table, "ALBUM_NUM_ITEMS", get_album_objects_count (s3)); g_free (s3); } } if (img_src) g_free (img_src); if (thumb) g_free (thumb); break; case INDEX_ITEM_TYPE_SEPARATOR: s1 = strdup (buf_img_separator); replace_table_add_key (local_replace_table, "LIST_SEPARATOR_TITLE", item->title); break; } bb = TRUE; if (s1) { replace_table_process (&s1, local_replace_table); bb = fputs (s1, fout); free (s1); } replace_table_free (local_replace_table); if (! bb) { fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; break; } } continue; /* no need to write anything */ } /* Select apropriate line buffer */ if (strstr (buffer, "") && items->parent_index && setup->show_go_up) { b = strdup (buf_go_up_string); } else b = strdup (buffer); /* Replace all known tags */ replace_table_process (&b, global_replace_table); /* Write to file */ bb = fputs (b, fout); free (b); if (! bb) { fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; break; } } fclose (fout); fclose (fin); replace_table_free (global_replace_table); free (buffer); free (buf_img_list_landscape); free (buf_img_list_portrait); free (buf_img_separator); free (buf_go_up_string); return res; } /* * write_html_image: process single image template file * * template_src = template file of the album/index * original_img = source image file (original full-size) to get EXIF data from * dst = save generated file as * item = data for the current item * parent_items = array of items in the album, to determine our position and make links to previous/next image * */ gboolean write_html_image (TGallerySetup *setup, const char *template_src, const char *original_img, const char *dst, TIndexItem *item, TAlbum *parent_items) { #define BUFFER_SIZE 65536 FILE *fin; FILE *fout; char *buffer; char *big_dst; char *orig_dst; char *buf_img_fullsize_link; char *buf_exif_table; gboolean in_img_fullsize_link; gboolean in_exif_table; TExifData *exif; unsigned long img_big_w, img_big_h, img_orig_w, img_orig_h; unsigned int item_index, real_item_index, real_total_items; TIndexItem *previous_item; TIndexItem *next_item; TIndexItem *tmp_item; TAlbum *parent; int i; char *s1, *s2, *s3, *s4, *s5; char *imgname, *preload_imgname; char *title, *title_desc; char *b; gboolean res, bb; int level, old_parent_item_index; gboolean override_title_meta; gboolean image_fullsize; ReplaceTable *replace_table; fin = fopen (template_src, "r"); if (fin == NULL) { fprintf (stderr, "write_html_image: error reading file \"%s\": %s\n", template_src, strerror (errno)); return FALSE; } fout = fopen (dst, "w"); if (fout == NULL) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); fclose (fin); return FALSE; } buffer = malloc (BUFFER_SIZE); preload_imgname = NULL; replace_table = replace_table_new (); /* Get our index in the album */ item_index = 0; real_item_index = 0; real_total_items = 0; for (i = 0; i < parent_items->items->len; i++) { tmp_item = g_ptr_array_index (parent_items->items, i); if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE) { if (! item_index) real_item_index++; real_total_items++; } if (tmp_item == item) item_index = i + 1; } /* Get previous and next items */ previous_item = NULL; next_item = NULL; for (i = item_index - 2; i >= 0 && (previous_item == NULL || previous_item->type != INDEX_ITEM_TYPE_PICTURE); i--) previous_item = g_ptr_array_index (parent_items->items, i); if (previous_item && previous_item->type != INDEX_ITEM_TYPE_PICTURE) previous_item = NULL; for (i = item_index; item_index < parent_items->items->len && (next_item == NULL || next_item->type != INDEX_ITEM_TYPE_PICTURE); i++) next_item = g_ptr_array_index (parent_items->items, i); if (next_item && next_item->type != INDEX_ITEM_TYPE_PICTURE) next_item = NULL; /* Paths setup */ imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path); if (next_item && setup->preload) { s1 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); preload_imgname = g_strconcat (IMG_BIG_DIR, "/", s1, NULL); g_free (s1); } s1 = g_path_get_dirname (dst); big_dst = g_strconcat (s1, "/", IMG_BIG_DIR, "/", imgname, NULL); orig_dst = g_strconcat (s1, "/", IMG_ORIG_DIR, "/", imgname, NULL); g_free (s1); buf_img_fullsize_link = malloc (BUFFER_SIZE); memset (buf_img_fullsize_link, 0, BUFFER_SIZE); in_img_fullsize_link = FALSE; buf_exif_table = malloc (BUFFER_SIZE); memset (buf_exif_table, 0, BUFFER_SIZE); in_exif_table = FALSE; res = TRUE; /* Get EXIF data from the original image */ if (get_exif (original_img, &exif)) fprintf (stderr, "write_html_image: error getting exif data from file \"%s\"\n", orig_dst); /* Retrieve image sizes of preview and original image */ get_image_sizes (big_dst, &img_big_w, &img_big_h); image_fullsize = item->force_fullsize || (parent_items->fullsize && ! item->force_nofullsize) || (! item->force_nofullsize && ! parent_items->nofullsize && ! setup->nofullsize); if (image_fullsize) get_image_sizes (orig_dst, &img_orig_w, &img_orig_h); /* Get title and description from IPTC/EXIF/JPEG if not defined */ title = g_strdup (item->title); title_desc = g_strdup (item->title_description); if (setup->use_iptc_exif && title == NULL && title_desc == NULL) { if (exif->iptc_caption) title = g_strdup (exif->iptc_caption); if (exif->jpeg_comment) { if (! title) title = g_strdup (exif->jpeg_comment); else title_desc = g_strdup (exif->jpeg_comment); } if (exif->exif_imgdesc) { if (! title) title = g_strdup (exif->exif_imgdesc); /* if (! title_desc) -- disabled title_desc = g_strdup (exif->exif_imgdesc); */ } if (exif->exif_usercomment) { if (! title) title = g_strdup (exif->exif_usercomment); if (! title_desc) title_desc = g_strdup (exif->exif_usercomment); } /* Convert line breaks to be visible in the HTML code */ if (title) { str_replace (&title, "\r\n", "
"); str_replace (&title, "\n", "
"); } if (title_desc) { str_replace (&title_desc, "\r\n", "
"); str_replace (&title_desc, "\n", "
"); } } if (title) title = g_strstrip (title); if (title_desc) title_desc = g_strstrip (title_desc); /* Page title */ s1 = (title && strlen (title) > 0) ? g_strdup_printf("%s | ", title) : NULL; s2 = g_strdup_printf ("%s [%d/%d]", parent_items->title ? parent_items->title : parent_items->ID, real_item_index, real_total_items); s3 = setup->site_title ? g_strdup_printf(" | %s", setup->site_title) : NULL; s4 = g_strconcat (s1 ? s1 : "", s2, s3 ? s3 : "", NULL); replace_table_add_key (replace_table, "PAGE_TITLE", s4); if (s1) g_free (s1); if (s2) g_free (s2); if (s3) g_free (s3); g_free (s4); /* Simple placeholders */ replace_table_add_key (replace_table, "FILE_NAME", imgname); replace_table_add_key (replace_table, "TITLE", title); replace_table_add_key (replace_table, "DESCRIPTION", title_desc); replace_table_add_key (replace_table, "FOOTER", setup->footer); replace_table_add_key_int (replace_table, "TOTAL_ITEMS", real_total_items); replace_table_add_key_int (replace_table, "FILE_NO", real_item_index); replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_W", img_big_w); replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_H", img_big_h); replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w); replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h); replace_table_add_key_printf (replace_table, "IMG_SRC_BIG", "%s/%s", IMG_BIG_DIR, imgname); replace_table_add_key_printf (replace_table, "IMG_SRC_FULL", "%s/%s", IMG_ORIG_DIR, imgname); replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : ""); /* Navigation bar (NOTE: 'int level' is used below + favicon) */ s1 = g_strdup (imgname); parent = parent_items; old_parent_item_index = -1; level = 0; while (parent) { s3 = make_string ("../", level); s4 = g_strdup (parent->ID); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", parent == parent_items ? item_index : old_parent_item_index) : g_strdup (""); s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); free (s3); g_free (s1); g_free (s4); g_free (s5); s1 = s2; old_parent_item_index = parent->parent_item_index + 1; parent = parent->parent_index; level++; if (parent) { tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) level += count_dir_levels (tmp_item->path) - 1; } } replace_table_add_key (replace_table, "NAV_BAR", s1); g_free (s1); /* Supportfiles path */ s1 = make_string ("../", level - 1); replace_table_add_key (replace_table, "TEMPLATES_PATH", setup->support_files_use_common_root ? s1 : ""); free (s1); /* EXIF values */ replace_table_add_key (replace_table, "EXIF_ISO", exif->iso ? exif->iso : "??"); replace_table_add_key (replace_table, "EXIF_TIME", exif->exposure ? exif->exposure : "??"); replace_table_add_key (replace_table, "EXIF_APERTURE", exif->aperture ? exif->aperture : "??"); replace_table_add_key (replace_table, "EXIF_FOCAL_LENGTH", exif->focal_length ? exif->focal_length : "??"); replace_table_add_key (replace_table, "EXIF_FLASH", exif->flash ? exif->flash : "??"); replace_table_add_key (replace_table, "EXIF_DATE", exif->datetime ? exif->datetime : "??"); replace_table_add_key (replace_table, "EXIF_CAMERA_MODEL", exif->camera_model ? exif->camera_model : "??"); s1 = g_strdup_printf ("(%s)", exif->focal_length_35mm); replace_table_add_key (replace_table, "EXIF_FOCAL_35", exif->focal_length_35mm ? s1 : ""); g_free (s1); /* Border style */ s1 = item->border_style; if (s1 == NULL) s1 = parent_items->border_style; if (s1 == NULL) s1 = setup->border_style; if (s1 == NULL) s1 = "border_single"; replace_table_add_key (replace_table, "IMG_BORDER_STYLE", s1); /* Next/Previous links */ if (next_item) { s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); replace_table_add_key_printf (replace_table, "LINK_NEXT", "%s.html", s2); g_free (s2); } else replace_table_add_key (replace_table, "LINK_NEXT", "index.html"); if (previous_item) { s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path); replace_table_add_key_printf (replace_table, "LINK_PREV", "%s.html", s2); g_free (s2); } else replace_table_add_key (replace_table, "LINK_PREV", "index.html"); /* META tags */ override_title_meta = setup->use_title_as_meta && title && (strlen (title) > 0); s1 = g_strdup_printf ("\t\n", VERSION); if (setup->meta_author || parent_items->meta_author) { s3 = g_strdup (parent_items->meta_author ? parent_items->meta_author : setup->meta_author); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_description || parent_items->meta_description || override_title_meta) { s3 = g_strdup (override_title_meta ? title : (parent_items->meta_description ? parent_items->meta_description : setup->meta_description)); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if ((setup->meta_keywords || parent_items->meta_keywords) && (! override_title_meta)) { s3 = g_strdup (parent_items->meta_keywords ? parent_items->meta_keywords : setup->meta_keywords); adjust_tags_parameter (&s3); s2 = g_strdup_printf ("%s\t\n", s1, s3); g_free (s3); g_free (s1); s1 = s2; } if (setup->favicon_file && strlen (setup->favicon_file) > 0) { s3 = make_string ("../", level - 1); if (setup->favicon_type) s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); else s2 = g_strdup_printf ("%s\t\n", s1, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); g_free (s1); s1 = s2; } replace_table_add_key (replace_table, "CGG_META_TAGS", s1); g_free (s1); /* Read through the template and replace placeholders with real data */ while (! feof (fin)) { memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; /* Block placeholders */ if (strstr (buffer, "")) { in_img_fullsize_link = TRUE; continue; } if (strstr (buffer, "")) { in_img_fullsize_link = FALSE; if (! image_fullsize) /* write down the block later in this cycle */ continue; } if (strstr (buffer, "")) { in_exif_table = TRUE; continue; } if (strstr (buffer, "")) { in_exif_table = FALSE; if (! setup->show_exif_table) continue; } if (in_img_fullsize_link) { buf_img_fullsize_link = strncat (buf_img_fullsize_link, buffer, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2); continue; } if (in_exif_table) { buf_exif_table = strncat (buf_exif_table, buffer, BUFFER_SIZE - strlen (buf_exif_table) - 2); continue; } /* Select apropriate line buffer */ if (strstr (buffer, "") && image_fullsize) { b = strdup (buf_img_fullsize_link); } else if (strstr (buffer, "") && setup->show_exif_table) { b = strdup (buf_exif_table); } else b = strdup (buffer); /* Replace all known tags */ replace_table_process (&b, replace_table); /* Write to file */ bb = fputs (b, fout); free (b); if (! bb) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; break; } } fclose (fout); fclose (fin); if (title) g_free (title); if (title_desc) g_free (title_desc); free (buffer); free (big_dst); free (orig_dst); free (buf_img_fullsize_link); free (buf_exif_table); g_free (imgname); if (preload_imgname) g_free (preload_imgname); free_exif_data (exif); replace_table_free (replace_table); return res; } static void mirror_files (TGallerySetup *setup, char **files, const char *src_tree, const char *dst_dir, const char *label) { char **extra; char *s1, *s2, *s3; if (files && g_strv_length (files) > 0) { extra = files; if (setup->verbose) printf ("%s", label); while (*extra) { s1 = g_strstrip (*extra); if (strlen (s1) > 0) { /* First create target directory if it doesn't exist */ s2 = g_strconcat (dst_dir, "/", s1, NULL); s3 = g_path_get_dirname (s2); g_free (s2); if (g_mkdir_with_parents (s3, DEFAULT_DATA_DIR_MODE)) { fprintf (stderr, "error making target extra directory '%s': %s\n", s3, strerror (errno)); g_free (s3); continue; } g_free (s3); /* Copy the file */ if (setup->verbose) printf (" %s", s1); s2 = g_strconcat (src_tree, "/", s1, NULL); s3 = g_strconcat (dst_dir, "/", s1, NULL); copy_file (s2, s3); g_free (s2); g_free (s3); } extra++; } if (setup->verbose) printf ("\n"); } } /* * build_tree: generate complete gallery tree based on source xml files * * src_tree = source directory of the items * dst_dir = destination of the generated items * parent_index = parent album to determine our descent in the tree * */ gboolean build_tree (TGallerySetup *setup, const char *src_tree, const char *dst_dir, TAlbum *parent_index, int parent_item_index) { char *idx_file; TAlbum *items; TIndexItem *item; char *s1, *s2, *s3; char *thumb_dir; char *img_big_dir; char *img_orig_dir; char *template; char *imgname; gboolean res; int i; printf ("*** Entering directory '%s'\n", src_tree); #ifdef __DEBUG_ALL__ printf ("setup->real_templates_dir = %s\n", setup->real_templates_dir); #endif /* Check access permissions */ if (access (src_tree, R_OK)) { fprintf (stderr, "error accessing source directory: %s\n", strerror (errno)); return FALSE; } if (access (dst_dir, R_OK | W_OK | X_OK)) { if (g_mkdir_with_parents (dst_dir, DEFAULT_DATA_DIR_MODE)) { fprintf (stderr, "error creating destination directory: %s\n", strerror (errno)); return FALSE; } } /* Check the index file */ idx_file = g_strconcat (src_tree, "/index.xml", NULL); if (access (idx_file, R_OK)) { fprintf (stderr, "error accessing index file '%s': %s\n", idx_file, strerror (errno)); g_free (idx_file); return FALSE; } /* Read the index file and fill items array */ items = malloc (sizeof (TAlbum)); memset (items, 0, sizeof (TAlbum)); if (! parse_album_xml (idx_file, items)) { fprintf (stderr, "error reading index file '%s'\n", idx_file); g_free (idx_file); free_album_data (items); return FALSE; } g_free (idx_file); items->parent_index = parent_index; items->parent_item_index = parent_item_index; /* Copy support files */ if (! setup->support_files_use_common_root || parent_index == NULL) { /* copy only if we're in root level or old-style is active */ mirror_files (setup, setup->template_files, setup->real_templates_dir, dst_dir, "Copying template files: "); /* favicon */ if (setup->favicon_file && strlen (setup->favicon_file) > 0) { if (setup->verbose) printf ("Copying favicon: %s\n", setup->favicon_file); s3 = g_path_get_dirname (setup->setup_xml_path); s1 = g_strconcat (s3, "/", setup->favicon_file, NULL); s2 = g_strconcat (dst_dir, "/", setup->favicon_file, NULL); copy_file (s1, s2); g_free (s1); g_free (s2); g_free (s3); } } /* Prepare target thumbnail directory */ thumb_dir = g_strconcat (dst_dir, "/", THUMBNAIL_DIR, NULL); if (access (thumb_dir, R_OK | W_OK | X_OK)) if (g_mkdir_with_parents (thumb_dir, DEFAULT_DATA_DIR_MODE)) { fprintf (stderr, "error making target thumbnail directory: %s\n", strerror (errno)); g_free (thumb_dir); free_album_data (items); return FALSE; } g_free (thumb_dir); /* Prepare target preview and orig directories */ if (items->type == GALLERY_TYPE_ALBUM) { res = TRUE; img_big_dir = g_strconcat (dst_dir, "/", IMG_BIG_DIR, NULL); img_orig_dir = g_strconcat (dst_dir, "/", IMG_ORIG_DIR, NULL); if (access (img_big_dir, R_OK | W_OK | X_OK)) if (g_mkdir_with_parents (img_big_dir, DEFAULT_DATA_DIR_MODE)) { fprintf (stderr, "error making target preview directory: %s\n", strerror (errno)); res = FALSE; } if (access (img_orig_dir, R_OK | W_OK | X_OK)) if (g_mkdir_with_parents (img_orig_dir, DEFAULT_DATA_DIR_MODE)) { fprintf (stderr, "error making target full size directory: %s\n", strerror (errno)); res = FALSE; } g_free (img_big_dir); g_free (img_orig_dir); if (! res) { free_album_data (items); return FALSE; } } /* Start generating items */ if (items->type == GALLERY_TYPE_INDEX) template = g_strconcat ("/", setup->template_index, NULL); else if (items->type == GALLERY_TYPE_ALBUM) template = g_strconcat ("/", setup->template_album, NULL); else /* default to album */ template = g_strconcat ("/", setup->template_album, NULL); if (setup->verbose) printf ("Writing 'index.html' ...\n"); s1 = g_strconcat (setup->real_templates_dir, template, NULL); s2 = g_strconcat (dst_dir, "/index.html", NULL); res = write_html_album (setup, s1, s2, items); g_free (s1); g_free (s2); g_free (template); if (! res) { fprintf (stderr, "error generating target index file\n"); free_album_data (items); return FALSE; } if (setup->verbose) printf (" done.\n"); /* Recurse to sub-albums (in case of album index) */ if (items->type == GALLERY_TYPE_INDEX) { if (items->items->len > 0) { for (i = 0; i < items->items->len; i++) { item = g_ptr_array_index (items->items, i); if (item == NULL) { fprintf (stderr, "build_tree: error getting item %d\n", i); continue; } if (item->type == INDEX_ITEM_TYPE_PICTURE) { s1 = g_strconcat (src_tree, "/", item->path, "/", NULL); s2 = g_strconcat (dst_dir, "/", item->path, "/", NULL); build_tree (setup, s1, s2, items, i); g_free (s1); g_free (s2); } } } } /* Generate separate image pages (in case of album) */ if (items->type == GALLERY_TYPE_ALBUM) { if (items->items->len > 0) { for (i = 0; i < items->items->len; i++) { item = g_ptr_array_index (items->items, i); if (item == NULL) { fprintf (stderr, "build_tree: error getting item %d\n", i); continue; } if (item->type == INDEX_ITEM_TYPE_PICTURE) { imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path); if (setup->verbose) printf ("Writing '%s.html' ...", imgname); s1 = g_strconcat (setup->real_templates_dir, "/", setup->template_photo, NULL); s2 = g_strconcat (items->base_dir, "/", (item->path == NULL && item->preview) ? item->preview : item->path, NULL); s3 = g_strconcat (dst_dir, "/", imgname, ".html", NULL); res = write_html_image (setup, s1, s2, s3, item, items); g_free (s1); g_free (s2); g_free (s3); g_free (imgname); if (! res ) continue; if (setup->verbose) printf (" done.\n"); } } } } /* Copy extra files */ mirror_files (setup, items->extra_files, src_tree, dst_dir, "Copying extra files: "); printf ("*** Leaving directory '%s'\n", src_tree); free_album_data (items); return TRUE; }