显然:remove_attachment插件在检查和提交方面起到了作用,但我如何从控制器调用该方法?
我正在将Rails 5.2与Shrine gem一起用于图像上传。在客户端,我将NativeScript 6.0与Angular 8.0结合使用。
我已经安装了Shrine,并且可以在Rails端运行,并且可以通过Uppy直接上传。
在使用NativeScript的前端(Android移动设备)上,我可以拍摄一张照片(使用nativescript-camera),然后使用nativescript-background-http将其发送到nativescript-background-http演示服务器(基于节点)。
我遇到的问题是从NativeScript发送到Shrine。
在后端,我有这些路线
Rails.application.routes.draw do
resources :asset_items
mount ImageUploader.upload_endpoint(:cache) => "/images/upload" # POST /images/upload
end
Run Code Online (Sandbox Code Playgroud)
在神社里我有
require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"), }
Shrine.plugin :logging, logger: Rails.logger
Shrine.plugin :upload_endpoint
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Run Code Online (Sandbox Code Playgroud)
在前端
onTakePictureTap(args) {
requestPermissions().then(
() => {
var imageModule = require("tns-core-modules/ui/image");
takePicture({width: 150, height: 100, keepAspectRatio: true})
.then((imageAsset: any) => {
this.cameraImage = imageAsset;
let image = new imageModule.Image();
image.src …Run Code Online (Sandbox Code Playgroud) 我正在使用 ActiveStorage 上传 PDF 和图像。由于一些隐私问题,PDF 需要存储在本地,而图像需要使用 Amazon S3 存储。但是,ActiveStorage 看起来只支持为每个环境设置一种服务类型(除非您使用镜像功能,在这种情况下它不能满足我的需要)。
有没有办法在同一环境中使用不同的服务配置?例如,如果模型has_one_attached pdf使用本地服务:
local:
service: Disk
root: <%= Rails.root.join("storage") %>
Run Code Online (Sandbox Code Playgroud)
如果另一个模型has_one_attached image使用亚马逊服务:
amazon:
service: S3
access_key_id: ""
secret_access_key: ""
Run Code Online (Sandbox Code Playgroud) 我无法使用神rine为我的图像播种,与载波不同,以下代码不起作用。
Profile.create! id: 2,
user_id: 2,
brand: "The Revengers",
location: "Azgaurd",
phone_number: "send a raven",
image_data: File.open(Rails.root+"app/assets/images/seed/thor.png")
Run Code Online (Sandbox Code Playgroud)
我也尝试过
image_data: ImageUploader.new(:store).upload(File.open(Rails.root+"app/assets/images/seed/thor.png"))
Run Code Online (Sandbox Code Playgroud)
但它返回
JSON::ParserError in Profiles#show
743: unexpected token at '#<ImageUploader::UploadedFile:0x007fd8bc3142e0>'
Run Code Online (Sandbox Code Playgroud)
有圣地吗?我似乎在任何地方都找不到。
shrine.rb
require "cloudinary"
require "shrine/storage/cloudinary"
Cloudinary.config(
cloud_name: ENV['CLOUD_NAME'],
api_key:ENV['API_KEY'],
api_secret:ENV['API_SECRET'],
)
Shrine.storages = {
cache: Shrine::Storage::Cloudinary.new(prefix: "cache"), # for direct
uploads
store: Shrine::Storage::Cloudinary.new(prefix: "store"),
}
Run Code Online (Sandbox Code Playgroud)
profile.rb
class Profile < ApplicationRecord
include ImageUploader[:image]
belongs_to :user
has_and_belongs_to_many :genres
scoped_search on: [:brand]
end
Run Code Online (Sandbox Code Playgroud)
image_uploader.rb
class ImageUploader < Shrine
end
Run Code Online (Sandbox Code Playgroud) 我正在使用 Vips 通过 Shrine 调整图像大小,希望可以使用 Vips 库在图像顶部合并一层文本。
ImageProcessing::Vips.source(image).resize_to_fill!(width, height)
Run Code Online (Sandbox Code Playgroud)
这段代码效果很好,如何在 resize_to_fill 之后添加一层文本?
目标是用白色文本编写“Hello world”,并在图像中心放置 CSS 文本阴影。
我试过写这样的东西,但到目前为止我只收到错误:
Vips\Image::text('Hello world!', ['font' => 'sans 120', 'width' => $image->width - 100]);
Run Code Online (Sandbox Code Playgroud) 我已经苦苦挣扎了大约5个小时,试图了解Shrine为什么阻止了我的上传。我要么在强参数中收到诸如“神殿:无效文件”之类的错误,要么出现“预期数组但有字符串”之类的错误。如果没有错误,则实际上不会保存图像。
require "image_processing/mini_magick"
class ImageUploader < Shrine
include ImageProcessing::MiniMagick
plugin :activerecord
plugin :backgrounding
plugin :cached_attachment_data
plugin :determine_mime_type
plugin :delete_raw
plugin :direct_upload
plugin :logging, logger: Rails.logger
plugin :processing
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :versions
Attacher.validate do
validate_max_size 2.megabytes, message: 'is too large (max is 2 MB)'
validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
end
def process(io, context)
case context[:phase]
when :store
thumb = resize_to_limit!(io.download, 200, 200)
{ original: io, thumb: thumb }
end
end
end
Run Code Online (Sandbox Code Playgroud)
class Image < ActiveRecord::Base
include ImageUploader[:image] …Run Code Online (Sandbox Code Playgroud) 以下代码使用 RSpec 3.5 和用于文件上传的Shrine gem在 Rails 4.2 应用程序的模型规范内测试图像验证。
我的问题是:
文件上传设置的其他方面在控制器和功能规范中进行了测试,这些与此问题无关。
RSpec.describe ShareImage, :type => :model do
describe "#image", :focus do
let(:image_file) do
# Could not get fixture_file_upload to work, but that's irrelevant
Rack::Test::UploadedFile.new(File.join(
ActionController::TestCase.fixture_path, 'files', filename))
end
let(:share_image) { FactoryGirl.build(:share_image, image: image_file) }
before(:each) { share_image.valid? }
context "with a valid image file" do
let(:filename) { 'image-valid.jpg' }
it "attaches the image to this record" do
expect(share_image.image.metadata["filename"]).to eq filename
end
end
context "with JPG extension and …Run Code Online (Sandbox Code Playgroud) 我还有一些可以对图像执行的其他任务。例如选择多个图像,并将它们组合成一个图像。我让那部分与 RMagick 和本地文件一起工作,我让上传部分与 Shrine 一起工作,但我需要将两者连接起来。上传图像后(理想情况下,该解决方案应适用于本地文件系统存储和 S3),我如何才能再次访问该文件,以使用 ImageMagick/RMagick 对其进行操作?我假设如果我使用 S3,我需要将图像从 S3 DL 到服务器并临时存储它们?有没有其他方法可以做到这一点?
我正在开发一个需要上传视频的应用程序。我添加了 Shrine 和 s3 存储。
到这里一切正常。现在我需要对视频进行转码,并将以下代码添加到 video_uploader 文件中
class VideoUploader < Shrine
plugin :processing
plugin :versions
process(:store) do |io|
transcoder = Aws::ElasticTranscoder::Client.new(
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'us-east-1',
)
pipeline = transcoder.create_pipeline(options = {
:name => "name",
:input_bucket => "bucket",
:output_bucket => "bucket",
:role => "arn:aws:iam::XXXXX:role/Elastic_Transcoder_Default_Role",
})
PIPELINE_ID = pipeline[:pipeline][:id]
transcode_hd = transcoder.create_job({
:pipeline_id=>PIPELINE_ID,
:input=> {
:key=> "cache/"+io.id,
:frame_rate=> "auto",
:resolution => "auto",
:aspect_ratio => "auto",
:container => 'auto'
},
:outputs=>[{
:key=>"store/"+io.id,
:preset_id=>"1351620000001-000010",
}]
})
end
end
Run Code Online (Sandbox Code Playgroud)
转码正在工作,基本上是对上传到缓存文件夹的新文件进行转码,并放入同名的存储文件夹中。
现在的问题是将该文件附加到数据库中的记录中。截至目前,该记录已使用不同的名称进行更新,它会在 0mb …
我设法使用shrine将文件上传到s3,但是我试图根据它所属的相册将每张照片上传到不同的文件夹.
让我们说我有一个名为abc::
将图像上传到相册:family应将图像上传到:abc/family/...
将图像上传到相册:friends应将图像上传到:abc/friends/...
我没有找到Shrine.storages在初始化文件中执行此操作的方法.
我想这样做的方法是以某种方式使用default_storage和dynamic_storage插件,但我还没有成功.
任何建议/解决方案?
非常感谢 :)
关系:
Album has_many :photos
Photo belongs_to :album
Photo上课有image_data神社的场地.
我在初始化程序中的代码:(基本的东西)
s3_options = {
access_key_id: ENV["S3_KEY"],
secret_access_key: ENV["S3_SECRET"],
region: ENV["S3_REGION"],
bucket: ENV["S3_BUCKET"],
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我发现有一个名为的插件:pretty_location它添加了一个更好的文件夹结构,但它不是我需要的,它添加/Photo/:photo_id/image/:image_name在桶下,但我需要相册名称.
我刚刚在 s3 上创建了一个私有存储桶,用于包含用户个人资料图片。使用公共存储桶,所有图像都会正确缓存(之前的回形针配置具有相同的设置)。
我有以下神社初始值设定项:
s3_options = {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: ENV['AWS_REGION'],
bucket: ENV['S3_BUCKET_NAME']
}
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new('tmp', prefix: 'uploads/cache'),
store: Shrine::Storage::S3.new(**s3_options)
}
Shrine.plugin :activerecord
Shrine.plugin :logging
Shrine.plugin :determine_mime_type
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :delete_promoted
Shrine.plugin :delete_raw
Shrine.plugin :remove_invalid
Run Code Online (Sandbox Code Playgroud)
以及以下上传者:
class AvatarUploader < Shrine
plugin :pretty_location
plugin :processing
plugin :upload_options, store: {
acl: 'private',
cache_control: "max-age=604800",
}
end
Run Code Online (Sandbox Code Playgroud)
s3 对象上的 CacheControl 已正确设置为 1 周,并且在响应中可以看到相同的情况。我注意到,在每个请求上,签名的 url 在X-Amz-Signature哈希方面都不同,这很可能导致缓存未命中(每个请求的 Etag 都相同)。我认为这就是它不起作用的原因,但我不知道如何在对象未过期的情况下使 X-Amz-Signature 相同。
当我们调用该方法时url,X-AMZ-Expires可以使用参数 定义for 的值expires_in。
有没有办法在 Shrine 的配置文件或上传器中定义它,以便每次调用时url都不需要给出 的值expired_in?
shrine ×12
ruby-on-rails ×11
amazon-s3 ×5
ruby ×2
angular ×1
aws-sdk ×1
aws-sdk-ruby ×1
file-upload ×1
imagemagick ×1
mobile ×1
nativescript ×1
testing ×1
vips ×1