使用Paperclip根据内容类型更改上载文件的路径

时间:2016-04-06 14:05:48

标签: ruby-on-rails paperclip

我正在使用Paperclip来处理我的应用程序的文件上传。我需要能够根据文件的stack level too deep更改文件的路径。当我尝试使用lambda执行此操作时,我收到has_attached_file :file, :storage => :s3, path: lambda { |a| a.instance.determine_path_for_content }, :s3_credentials => { :bucket => 'myBucket', :access_key_id => ENV['AWS_ACCESS_KEY'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] } def determine_path_for_content if file.instance.file_content_type == "video/mp4" "/:name/:event:event_id/:basename_time_:start_mp4_at_end_time:fingerprint.:extension" else "/:name/:event:event_id/:basename_:fingerprint.:extension" end end 错误。

sap.ui.define("my/own/Control",[
    "sap/m/VBox",
    "sap/m/Label",
    "sap/m/Input",
], function (VBox, Label, Input) {
    "use strict";
    var Control = VBox.extend("my.own.Control", {
        metadata : {
            properties : {
                label : {
                    type: 'string',
                    defaultValue: 'kiko'
                },
                value : {
                    type: 'string', 
                    defaultValue: 'lol'
                }
            },
            aggregations : {
                _label: {
                    type: 'sap.m.Label',
                    multiple: false,
                    visibility: 'hidden'
                },
                _value : {
                    type: 'sap.m.Input',
                    multiple: false,
                    visibility: 'hidden'
                }
            }
        },
        renderer : function(oRm, oControl) {
            oRm.write('<div');
            oRm.writeControlData(oControl);
            oRm.writeClasses();
            oRm.write('>');
            oRm.renderControl(oControl.getAggregation('_label'));
            oRm.renderControl(oControl.getAggregation('_value'));
            oRm.write('</div>');
        }
    });

    Control.prototype.init = function() {
        VBox.prototype.init.call(this);
        this.setAggregation('_label', new Label({ text: this.getProperty('label') }));
        this.setAggregation('_value', new Input({ value: this.getProperty('value') }));
    };

    Control.prototype.setLabel = function(l) {
        this.setProperty('label', l, true)
        this.getAggregation('_label').setText(l);
    };

    Control.prototype.setValue = function(v) {
        this.setProperty('value', v, true)
        this.getAggregation('_value').setValue(v);
    };

    return Control;
});

1 个答案:

答案 0 :(得分:2)

试试这个:

has_attached_file :file, :storage => :s3,
                          path: lambda { |a| a.class.determine_path_for_content(file.instance.file_content_type) },
                          :s3_credentials => { 
                          :bucket => 'myBucket', 
                          :access_key_id => ENV['AWS_ACCESS_KEY'],
                          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
                          }

def self.determine_path_for_content(content_type)
    if content_type == "video/mp4"
        "/:name/:event:event_id/:basename_time_:start_mp4_at_end_time:fingerprint.:extension"
    else
        "/:name/:event:event_id/:basename_:fingerprint.:extension"
    end
end

在我看来,最好将它作为一个类方法,并且应该避免你在这里的递归。