posts_controller.rb 3.35 KB
class PostsController < ApplicationController

  before_filter :current_user, except: [:sign_in]
  # GET /posts
  # GET /posts.json
  def index
    sort = []
    params[:screa] ||= 'desc'
    {scate: :cate, schan: :chan, screa: :created_at}.each do |key, val|
      if params[key].present?
        sort.push(params[key] == "desc" ? "#{val} desc" : "#{val} asc")
      end
    end
    @posts = Post.where(isdel: false).page(params[:page]).per(20).order(sort.join(', '))

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find_by_flag(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find_by_flag(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: '文章创建成功.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find_by_flag(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: '文章修改成功.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find_by_flag(params[:id])
    @post.update_attributes(isdel: true) if @post

    redirect_to :back
  end

  # 上传图片
  def upload
    if params[:upload_file].present?
      upload = params[:upload_file]
      ext    = File.extname(upload.original_filename).downcase
      picture = Picture.create(filename: upload.original_filename)
      system("convert #{upload.tempfile.path} -auto-orient -resize '1000>' -gravity center -strip -quality 85 -format jpg public/uploads/#{picture.id}.jpg")
      # 有一个2:1.5的缩略图裁剪 最宽200
      # File.open("public/uploads/#{picture.id}#{ext}", "wb") { |f| f.write(upload.read) }
      render json: {success: true, msg: '上传成功', file_path: "/uploads/#{picture.id}.jpg"}
    else
      render json: {success: false, msg: '上传错误,联系管理员', file_path: '/images/logo.png'}
    end
  end

  def sign_in
    if request.post?
      if params[:username] == 'normz' and params[:password] == 'normz-nuozheng'
        session[:current_user] = 'normz'
        redirect_to posts_path
      else
        flash[:notice] = '账号或密码错误'
        redirect_to :sign_in
      end
    end
  end

  def sign_out
    session[:current_user] = nil
    redirect_to root_path
  end

  private

    def current_user
      unless session[:current_user] == 'normz'
        redirect_to :sign_in and return
      end
    end
end