You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							91 lines
						
					
					
						
							2.7 KiB
						
					
					
				
			
		
		
	
	
							91 lines
						
					
					
						
							2.7 KiB
						
					
					
				| class AvatarController < ApplicationController
 | |
|   
 | |
|      
 | |
|   #before_filter :set_cache_buster   
 | |
|   include AvatarHelper   
 | |
|       
 | |
|   def upload
 | |
|     # Make sure that API users get used to set this content type
 | |
|     # as it won't trigger Rails' automatic parsing of the request body for parameters
 | |
|     unless request.content_type == 'application/octet-stream'
 | |
|       render :nothing => true, :status => 406
 | |
|       return
 | |
|     end
 | |
| 
 | |
|     unless request.raw_post.nil?
 | |
|      @source_type = params[:source_type]
 | |
|       @source_id = params[:source_id]
 | |
|       @temp_file = request.raw_post
 | |
|       if @temp_file.size > 0
 | |
|         if @temp_file.respond_to?(:original_filename)
 | |
|           @image_file = @temp_file.original_filename
 | |
|           #image_file.force_encoding("UTF-8") if filename.respond_to?(:force_encoding)
 | |
|         else
 | |
|           @image_file=params[:filename]
 | |
|         end      
 | |
|       end
 | |
|     end
 | |
|     if @temp_file && (@temp_file.size > 0)
 | |
|       diskfile=disk_filename(@source_type,@source_id)
 | |
|       @urlfile='/' << File.join("images","avatars",avatar_directory(@source_type),avatar_filename(@source_id,@image_file))
 | |
|       logger.info("Saving avatar '#{diskfile}' (#{@temp_file.size} bytes)")
 | |
|       path = File.dirname(diskfile)
 | |
|       unless File.directory?(path)
 | |
|         FileUtils.mkdir_p(path)
 | |
|       end
 | |
|       md5 = Digest::MD5.new
 | |
|       File.open(diskfile, "wb") do |f|
 | |
|         if @temp_file.respond_to?(:read)
 | |
|           buffer = ""
 | |
|           while (buffer = @temp_file.read(8192))
 | |
|             f.write(buffer)
 | |
|             md5.update(buffer)
 | |
|           end
 | |
|         else
 | |
|         f.write(@temp_file)
 | |
|         md5.update(@temp_file)
 | |
|         end
 | |
|       end
 | |
| #    self.digest = md5.hexdigest
 | |
|     end
 | |
|     @temp_file = nil
 | |
|     # @avatar = Avatar.new(:receive_file => request.raw_post)
 | |
|     # @avatar.source_id = User.current.id
 | |
|     # @avatar.image_file = params[:filename].presence || Redmine::Utils.random_hex(16)
 | |
|     # saved = @avatar.save
 | |
|     begin
 | |
|       f = Magick::ImageList.new(diskfile)
 | |
|       width = 300.0;
 | |
|       proportion = (width/f[0].columns)
 | |
|       height = (f[0].rows*proportion)
 | |
|       f.resize_to_fill!(width,height)
 | |
|       # f.scale!(width,height)
 | |
|       f.write(diskfile)
 | |
|     rescue Exception => e
 | |
|       logger.error "[Error] avatar : avatar_controller#upload ===> #{e}"
 | |
|     end
 | |
| 
 | |
|     
 | |
|     respond_to do |format|
 | |
|       format.js
 | |
|       format.api {
 | |
|         if saved
 | |
|           render :action => 'upload', :status => :created
 | |
|         else
 | |
|           render_validation_errors(@avatar)
 | |
|         end
 | |
|       }
 | |
|     end
 | |
|   end
 | |
|   
 | |
|   private
 | |
|   
 | |
|     def set_cache_buster  
 | |
|       response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"  
 | |
|       response.headers["Pragma"] = "no-cache"  
 | |
|       response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"  
 | |
|     end 
 | |
| 
 | |
| 
 | |
| end
 |