Class: Gallerist::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/gallerist/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_default_middleware(builder) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/gallerist/app.rb', line 143

def self.setup_default_middleware(builder)
  builder.use Sinatra::ExtendedRack
  builder.use Gallerist::ShowExceptions if show_exceptions?
  builder.use Gallerist::RaiseWarmupExceptions

  setup_logging builder
end

Instance Method Details

#libraryObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gallerist/app.rb', line 68

def library
  unless settings.respond_to? :library
    settings.set :library, Gallerist::Library.new(settings.library_path)

    logger.info "Loading library from \"#{library.path}\""

    if settings.copy_dbs
      logger.debug 'Creating temporary copy of the main library database...'
      library.copy_base_db
      logger.debug '  Completed.'
    end

    ActiveRecord::Base.establish_connection({
      adapter: 'sqlite3',
      database: library.library_db
    })
    ActiveRecord::Base.connection.exec_query 'PRAGMA journal_mode="MEMORY";'

    if settings.copy_dbs
      logger.debug 'Creating temporary copies of additional library databases...'
      library.copy_extra_dbs
      logger.debug '  Completed.'
    end

    logger.info "  Found library with type '%s'." % [ library.app_id ]

    Gallerist.load_models
    Gallerist::BaseModel.descendants.each do |model|
      logger.debug "Setting up %s for library type '%s'" % [ model, library.type ]

      model.setup_for library.type
    end

    Gallerist::ImageProxiesModel.establish_connection({
      adapter: 'sqlite3',
      database: library.image_proxies_db
    })
    Gallerist::ImageProxiesModel.connection.exec_query 'PRAGMA journal_mode="MEMORY";'

    Gallerist::PersonModel.establish_connection({
      adapter: 'sqlite3',
      database: library.person_db
    })
    Gallerist::PersonModel.connection.exec_query 'PRAGMA journal_mode="MEMORY";'
  end
  settings.library
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gallerist/app.rb', line 116

def navbar_for(obj)
  @navbar = [[ '/', library.name ]]

  case obj
  when :all_photos
    @navbar << [ '/photos', 'All photos' ]
  when :favorites
    @navbar << [ '/favorites', 'Favorites' ]
  when Gallerist::Album
    @navbar << [ '/albums', 'Albums' ]
    @navbar << [ '/albums/%s' % [ obj.id ], obj.name ]
  when Gallerist::Person
    @navbar << [ '/persons', 'Persons' ]
    @navbar << [ '/persons/%s' % [ obj.id ], obj.name ]
  when Gallerist::Tag
    @navbar << [ '/tags', 'Tags' ]
    @navbar << [ '/tags/%s' % [ obj.simple_name ], obj.name ]
  end
end

#photo(id) ⇒ Object



136
137
138
139
140
141
# File 'lib/gallerist/app.rb', line 136

def photo(id)
  Gallerist::Photo.find id
rescue ActiveRecord::RecordNotFound
  logger.error 'Could not find the photo with ID #%s.' % [ id ]
  not_found
end

#send_library_file(file, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gallerist/app.rb', line 56

def send_library_file(file, options = {})
  logger.debug "Serving file '%s' from library..." % [ file ]

  file_path = File.join(library.path, file)
  response = catch(:halt) { send_file file_path, options }
  if response == 404
    logger.error "File '%s' could not be served, because it does not exist." % file
  end

  halt response
end