2024-11-26 08:46:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Api::V1::PhotosController < ApiController
|
2024-12-04 06:32:13 -05:00
|
|
|
before_action :check_integration_configured, only: %i[index thumbnail]
|
|
|
|
|
before_action :check_source, only: %i[thumbnail]
|
|
|
|
|
|
2024-11-26 08:46:26 -05:00
|
|
|
def index
|
|
|
|
|
@photos = Rails.cache.fetch("photos_#{params[:start_date]}_#{params[:end_date]}", expires_in: 1.day) do
|
2024-12-04 07:45:19 -05:00
|
|
|
Photos::Search.new(current_api_user, start_date: params[:start_date], end_date: params[:end_date]).call
|
2024-11-26 11:36:22 -05:00
|
|
|
end
|
2024-11-26 08:46:26 -05:00
|
|
|
|
|
|
|
|
render json: @photos, status: :ok
|
|
|
|
|
end
|
2024-11-26 10:36:02 -05:00
|
|
|
|
|
|
|
|
def thumbnail
|
2024-12-03 07:50:05 -05:00
|
|
|
response = fetch_cached_thumbnail(params[:source])
|
2024-12-02 12:21:12 -05:00
|
|
|
handle_thumbnail_response(response)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-12-03 07:50:05 -05:00
|
|
|
def fetch_cached_thumbnail(source)
|
2024-12-02 12:21:12 -05:00
|
|
|
Rails.cache.fetch("photo_thumbnail_#{params[:id]}", expires_in: 1.day) do
|
2024-12-03 08:44:24 -05:00
|
|
|
Photos::Thumbnail.new(current_api_user, source, params[:id]).call
|
2024-11-26 10:36:02 -05:00
|
|
|
end
|
2024-12-02 12:21:12 -05:00
|
|
|
end
|
2024-11-26 10:36:02 -05:00
|
|
|
|
2024-12-02 12:21:12 -05:00
|
|
|
def handle_thumbnail_response(response)
|
2024-11-26 10:36:02 -05:00
|
|
|
if response.success?
|
2024-12-02 12:21:12 -05:00
|
|
|
send_data(response.body, type: 'image/jpeg', disposition: 'inline', status: :ok)
|
2024-11-26 10:36:02 -05:00
|
|
|
else
|
|
|
|
|
render json: { error: 'Failed to fetch thumbnail' }, status: response.code
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-12-03 07:50:05 -05:00
|
|
|
|
|
|
|
|
def integration_configured?
|
2024-12-04 06:32:13 -05:00
|
|
|
current_api_user.immich_integration_configured? || current_api_user.photoprism_integration_configured?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_integration_configured
|
|
|
|
|
unauthorized_integration unless integration_configured?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_source
|
|
|
|
|
unauthorized_integration unless params[:source] == 'immich' || params[:source] == 'photoprism'
|
2024-12-03 07:50:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def unauthorized_integration
|
2024-12-03 10:05:38 -05:00
|
|
|
render json: { error: "#{params[:source]&.capitalize} integration not configured" },
|
2024-12-03 07:50:05 -05:00
|
|
|
status: :unauthorized
|
|
|
|
|
end
|
2024-11-26 08:46:26 -05:00
|
|
|
end
|