Rails 使用 mini_magick 保存的图片如何更改文件权限

tiseheaini · October 01, 2014 · Last by dddd1919 replied at October 10, 2014 · 3136 hits

使用 mini_magick 保存的图片权限不对,nginx 没办法正确读取。

代码如下

def save_picture
  upload_file = params[:picture]
  pic_name = [SecureRandom.hex(5), File.extname(upload_file.original_filename)].join
  image = MiniMagick::Image.read(upload_file.tempfile.read)
  image.write File.join( Rails.root, 'public', 'pictures', pic_name )

  render json: { status: true }
end
ll public/pitcures/
total 2688
-rw-------  1 tiny  staff   628K  9 30 23:52 0949edc801.png
-rw-------  1 tiny  staff    19K  9 30 23:07 24a2727d79.jpg
-rw-------  1 tiny  staff   400K  9 30 23:51 522a93502b.png
-rw-------  1 tiny  staff    19K  9 30 23:08 6b8a2001d4.jpg

使用 image.write 保存的图片权限不对,希望改成 644,没找到合适的方法。大家有好方法推荐吗?

直接用 linux 下的 chmod 命令就可以了

FileUtils.chmod("+r", File.join(full_path, file_name))

#1 楼 @robot_zhang 我尝试了一下,可以通过,我比较担心在使用更改权限时出现问题,我的担心是多余的

冷门问题竟然也有难友,怎么由 imagemagick 自己来搞定呢?

#3 楼 @dddd1919 我用 FileUtils.chmod 方法解决掉了,测试的时候没出现问题,当出现问题时再去解决它,时间成本很高的

#4 楼 @tiseheaini 好像也只有 open 之后直接 write 文件权限才会变成 600,我在中间加了个 image.quality(image['%Q']),然后 write 出来的文件就是 644 了. BTW,不用处理图片还是直接 File 来操作吧

You need to Sign in before reply, if you don't have an account, please Sign up first.