fabian
July 15, 2022, 7:40am
1
Hey Dradis-Team,
is there a way to pass the user as argument to an upload-plugin? As far as I can see, the framework “only” passes the path to the file. The uploader I’m currently building checks if all relevant content blocks exist in the project, adds them accordingly, and if the block doesn’t exist, it creates it. But content_service.create_content_block
needs a user_id
. My workaround at the moment is that it uses a hard coded user_id
.
etd
July 18, 2022, 8:18am
2
Hey @fabian ,
Your Importer should receive a :default_user_id parameter you can use for this. We’re setting it up in the UploadController:
uid: params[:item_id].to_i
)
end
def process_upload_inline(args={})
attachment = args[:attachment]
job_logger.write('Small attachment detected. Processing in line.')
begin
importer = @uploader::Importer.new(
default_user_id: current_user.id,
logger: job_logger,
plugin: @uploader,
project_id: current_project.id
)
importer.import(file: attachment.fullpath)
rescue Exception => e
# Fail noisily in test mode; re-raise the error so the test fails:
raise if Rails.env.test?
job_logger.write('There was a fatal error processing your upload:')
Or for larger jobs that get processed in the background in the UploadJob:
def perform(default_user_id:, file:, plugin_name:, project_id:, uid:)
logger = Log.new(uid: uid)
logger.write { "Job id is #{job_id}." }
logger.write { 'Running Ruby version %s' % RUBY_VERSION }
logger.write { 'Worker process starting background task.' }
plugin = plugin_name.constantize
importer = plugin::Importer.new(
default_user_id: default_user_id,
logger: logger,
plugin: plugin,
project_id: project_id
)
importer.import(file: file)
logger.write { 'Worker process completed.' }
end
end
HTH,
Daniel