app/controllers/gifts_controller.rb
# ajax request - called from gifts/index page
# return gift in new_messages_buffer_div and return any error messages in tasks_errors table
# both in page header
# JS function insert_update_gifts will move new gift from div buffer to gifts table in html page
ajax request - called from gifts/index page
return gift in new_messages_buffer_div and return any error messages in tasks_errors table
both in page header
JS function insert_update_gifts will move new gift from div buffer to gifts table in html page
ajax request - called from gifts / index page return gift in new_messages_buffer_div and return any error messages in tasks_errors table both in page header JS function insert_update_gifts will move new gift from div buffer to gifts table in html page
def create
    @api_gifts = []
    logger.debug2 "request.format = #{request.format}"
    logger.debug2 "xhr? = #{xhr?}"
    if xhr? and params[:gift][:datatype] == 'json' and request.format == 'text/javascript'
      render :nothing => true
      return
    end
    begin
      return format_response_key '.not_logged_in' unless logged_in?
      users2 = @users.find_all { |u| !u.deleted_at }
      return format_response_key '.deleted_user' if users2.size == 0
      @users = users2 if @users.size != users2.size
      gift = Gift.new
      gift.direction = 'giver' if gift.direction.to_s == ''
      gift_file = params[:gift_file]
      picture = (gift_file.class.name == 'ActionDispatch::Http::UploadedFile')
      if picture
        filetype = FastImage.type(gift_file.path).to_s
        if !%w(jpg jpeg gif png bmp).index(filetype)
          add_error_key '.unsupported_filetype', :filetype => filetype
          picture = false 
        end
      end
      if picture and gift_file.size > 2.megabytes
        add_error_key '.file_is_too_big', :maxsize => '2 Mb'
        picture = false 
      end
      gift.valid?
      return format_response_text(gift.errors.full_messages.join(', ')) if gift.errors.size > 0
      @users.each do |user|
        api_gift = ApiGift.new
        api_gift.gid = gift.gid
        api_gift.provider = user.provider
        api_gift.user_id_giver = gift.direction == 'giver' ? user.user_id : nil
        api_gift.user_id_receiver = gift.direction == 'receiver' ? user.user_id : nil
        api_gift.picture = picture ? 'Y' : 'N'
        api_gift.api_picture_url = picture_url if picture 
        if !API_GIFT_PICTURE_STORE[user.provider]
          api_gift.picture = 'N'
          api_gift.api_picture_url = nil
        end
        gift.api_gifts << api_gift
      end
      gift.save!
      if picture
        Picture.create_parent_dirs :rel_path => picture_rel_path
        cmd = "mv #{gift_file.path} #{picture_full_os_path}"
        stdout, stderr, status = User.open4(cmd)
        if status != 0
          logger.error2 "mv: cmd = #{cmd}"
          logger.error2 "mv: stdout = #{stdout}, stderr = #{stderr}, status = #{status}"
          begin
            File.delete(picture_full_os_path) if File.exists?(picture_full_os_path)
            Picture.delete_empty_parent_dirs(:rel_path => picture_rel_path)
          rescue => e
            logger.error2 "mv: OS cleanup failed. error = #{e.message}"
          end
          add_error_key ".file_mv_error", :error => stderr
          gift.save!
          gift.api_gifts.each do |api_gift|
            api_gift.picture = 'N'
            api_gift.api_picture_url = nil
            api_gift.save!
          end
          picture = false
        else
          cmd = "chmod o+r #{picture_full_os_path}"
          stdout, stderr, status = User.open4(cmd)
          if status != 0
            logger.error2 "chmod: cmd = #{cmd}"
            logger.error2 "chmod: stdout = #{stdout}, stderr = #{stderr}, status = #{status}"
            begin
              File.delete(picture_full_os_path) if File.exists?(picture_full_os_path)
              Picture.delete_empty_parent_dirs(:rel_path => picture_rel_path)
            rescue => e
              logger.error2 "chmod: OS cleanup failed. error = #{e.message}"
            end
            add_error_key ".file_chmod_error", :error => stderr
            gift.save!
            gift.api_gifts.each do |api_gift|
              api_gift.picture = 'N'
              api_gift.api_picture_url = nil
              api_gift.save!
            end
            picture = false
          end
        end
      end
      add_task "delete_local_picture(#{gift.id})", 10 if picture and Picture.temp_app_url?(picture_url)
      api_gift = gift.api_gifts.sort_by { |a| a.picture }.last
      @api_gifts = ApiGift.where("id = ?", api_gift.id).includes(:gift)
      logger.debug2 "@errors.size = #{@errors.size}"
      logger.debug2 " @api_gifts.size = #{@api_gifts.size}"
      format_response
    rescue => e
      logger.error2 "Exception: #{e.message.to_s}"
      logger.error2 "Backtrace: " + e.backtrace.join("\n")
      @api_gifts = []
      format_response_key '.exception', :error => e.message
    end
  end
def create @api_gifts = [ ] logger . debug2 "request.format = #{request.format}" logger . debug2 "xhr? = #{xhr?}" if xhr? and params [ :gift ] [ :datatype ] == 'json' and request . format == 'text/javascript' render :nothing => true return end begin return format_response_key '.not_logged_in' unless logged_in? users2 = @users . find_all { | u | ! u . deleted_at } return format_response_key '.deleted_user' if users2 . size == 0 @users = users2 if @users . size != users2 . size gift = Gift . new gift . direction = 'giver' if gift . direction . to_s == '' gift_file = params [ :gift_file ] picture = ( gift_file . class . name == 'ActionDispatch::Http::UploadedFile' ) if picture filetype = FastImage . type ( gift_file . path ) . to_s if ! %w( jpg jpeg gif png bmp ) . index ( filetype ) add_error_key '.unsupported_filetype' , :filetype => filetype picture = false end end if picture and gift_file . size > 2 . megabytes add_error_key '.file_is_too_big' , :maxsize => '2 Mb' picture = false end gift . valid? return format_response_text ( gift . errors . full_messages . join ( ', ' ) ) if gift . errors . size > 0 @users . each do | user | api_gift = ApiGift . new api_gift . gid = gift . gid api_gift . provider = user . provider api_gift . user_id_giver = gift . direction == 'giver' ? user . user_id : nil api_gift . user_id_receiver = gift . direction == 'receiver' ? user . user_id : nil api_gift . picture = picture ? 'Y' : 'N' api_gift . api_picture_url = picture_url if picture if ! API_GIFT_PICTURE_STORE [ user . provider ] api_gift . picture = 'N' api_gift . api_picture_url = nil end gift . api_gifts << api_gift end gift . save! if picture Picture . create_parent_dirs :rel_path => picture_rel_path cmd = "mv #{gift_file.path} #{picture_full_os_path}" stdout , stderr , status = User . open4 ( cmd ) if status != 0 logger . error2 "mv: cmd = #{cmd}" logger . error2 "mv: stdout = #{stdout}, stderr = #{stderr}, status = #{status}" begin File . delete ( picture_full_os_path ) if File . exists? ( picture_full_os_path ) Picture . delete_empty_parent_dirs ( :rel_path => picture_rel_path ) rescue => e logger . error2 "mv: OS cleanup failed. error = #{e.message}" end add_error_key ".file_mv_error" , :error => stderr gift . save! gift . api_gifts . each do | api_gift | api_gift . picture = 'N' api_gift . api_picture_url = nil api_gift . save! end picture = false else cmd = "chmod o+r #{picture_full_os_path}" stdout , stderr , status = User . open4 ( cmd ) if status != 0 logger . error2 "chmod: cmd = #{cmd}" logger . error2 "chmod: stdout = #{stdout}, stderr = #{stderr}, status = #{status}" begin File . delete ( picture_full_os_path ) if File . exists? ( picture_full_os_path ) Picture . delete_empty_parent_dirs ( :rel_path => picture_rel_path ) rescue => e logger . error2 "chmod: OS cleanup failed. error = #{e.message}" end add_error_key ".file_chmod_error" , :error => stderr gift . save! gift . api_gifts . each do | api_gift | api_gift . picture = 'N' api_gift . api_picture_url = nil api_gift . save! end picture = false end end end add_task "delete_local_picture(#{gift.id})" , 10 if picture and Picture . temp_app_url? ( picture_url ) api_gift = gift . api_gifts . sort_by { | a | a . picture } . last @api_gifts = ApiGift . where ( "id = ?" , api_gift . id ) . includes ( :gift ) logger . debug2 "@errors.size = #{@errors.size}" logger . debug2 " @api_gifts.size = #{@api_gifts.size}" format_response rescue => e logger . error2 "Exception: #{e.message.to_s}" logger . error2 "Backtrace: " + e . backtrace . join ( "\n" ) @api_gifts = [ ] format_response_key '.exception' , :error => e . message end end
ajax request - called from gifts/index page
return gift in new_messages_buffer_div and return any error messages in tasks_errors table
both in page header
JS function insert_update_gifts will move new gift from div buffer to gifts table in html page
ajax request - called from gifts / index page return gift in new_messages_buffer_div and return any error messages in tasks_errors table both in page header JS function insert_update_gifts will move new gift from div buffer to gifts table in html page
# File upload plugin support - ignore rails JS post - data has already been send in a json request # check if user account is being deleted (do not create new gift) # initialize gift # continue post without picture # continue post without picture # add api_gifts - one api_gifts for each provider # api_gift_id will be added in post_on_<provider> tasks # api_picture_url may change in post_on_<provider> tasks if picture store is :api # temporary or perm local url # no picture store for this provider # this is - provider does not support picture uploads and local perm picture store is not being used # create dir # move uploaded file to location in perm or temp picture store # mv failed # OS cleanup # ignore OS cleanup errors - write message on log and continue # continue post without picture # mv ok - change file permissions # apache must have read access to image files # chmod failed # OS cleanup # ignore OS cleanup errors - write message on log and continue # continue post without picture # delete picture after posting on api wall(s) - priority = 10 # find api gift - api gift with picture is preferred