How to get the object url with alias name from aws s3 using CloudFront - ruby-on-rails

I am uploading files with unique id like 'd9127dfd01182afe7d34a37' as object name to amazon s3 and storing the file information with my local database including original name of the file. And I am using CloudFront url to download the file.
If I download the file using CloudFront url file name is d9127dfd01182afe7d34a37. But I need to change file name again to it's original name wich I have in my database. I don't want to download it. I want to give the url with original name to the client(WebUI) and client can download it through url.
serverside code
document_url = initialize_cloud_service(document.provider['primary']).get_object_url(document_id, expires_at, 'CloudFront' )
if document_url
item = {}
item['id'] = document['_id'].to_s
item['name'] = document['name']
item['mime_type'] = document['mime_type']
item['url'] = document_url
return {success: true, message: MESSAGES['get_url_succuss'],data: item}.to_json
end
client side code
download: function(response){
file = response.data
link = document.createElement('a');
link.download = file.name;
link.href = file.url;
link.click();
},
Is there any way to achieve this? Please help me out. I am using ruby on rails and mongodb as local database.
Thanks

I have achieved by doing the following changes
Server Side code
begin
expires_at = Time.now.to_i + 30.seconds.to_i
options = nil
selected_provider = provider || document.provider['primary']
case selected_provider
when "s3"
options = {"response-content-disposition" => "attachment; filename=#{document['name']}"}
downloadable_url = initialize_cloud_service(selected_provider).get_downloadable_url(document_id, expires_at, options)
when "google_cloud"
downloadable_url = initialize_cloud_service(selected_provider).get_downloadable_url(document_id, expires_at, options)
downloadable_url += "&response-content-disposition=attachment%3B%20filename%3D#{document['name']}"
end
item = {}
item['id'] = document['_id'].to_s
item['name'] = document['name']
item['mime_type'] = document['mime_type']
item['url'] = downloadable_url
return {success: true, message: MESSAGES['get_url_succuss'],data: item}.to_json
rescue Exception => e
puts 'Exception in download, message: ' + e.message
return {success: false, message: MESSAGES['default']}.to_json
end
client side code
download: function(response){
var hiddenIFrameID = 'hiddenDownloader',
iframe = document.getElementById(hiddenIFrameID);
if (iframe === null) {
iframe = document.createElement('iframe');
iframe.id = hiddenIFrameID;
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
iframe.src = response.data.url;
},

Related

Streaming a file from remote S3 to react client through rails API

The app already does this using Zipline and allowing to stream one zip file with all the files selected. But what I want to accomplish is that if only one file is being sent achieve the same behavior but instead of sending a compressed zip file sending the file as it is in S3 (with its respective extension such as docx, jpeg, xlsx, etc). So this is what I have so far.
controller
def to_zip_or_single
if params[:attachments].present?
fileNames = {}
files = Attachment.where(id: params[:attachments]
.split(','))
.map do |attachment|
file = attachment.is_image? ? AbstractFileStruct.new(attachment.upload_annotated) : attachment.upload
if !fileNames[attachment.name]
fileNames[attachment.name] = 0
else
fileNames[attachment.name] += 1
end
attachmentName = File.basename(attachment.name, File.extname(attachment.name))
attachmentName = if fileNames[attachment.name] > 0
attachmentName + " (#{fileNames[attachment.name]})" + File.extname(attachment.name)
else
attachmentName + File.extname(attachment.name)
end
[file, attachmentName]
end
end
filename = params[:filename].present? ? params[:filename] : 'attachments.zip'
if files.one?
headers['Content-Disposition'] = 'attachment; filename="' + files[0][1] + '"'
headers['Content-Type'] = 'application/octet-stream'
response.headers['Last-Modified'] = Time.now.httpdate
response.cache_control[:public] ||= false
response.sending_file = true
file = normalize(files[0][0])
the_remote_uri = URI(file[:url])
Net::HTTP.get_response(the_remote_uri) do |response|
# DO SOMETHING WITH THE RESPONSE
end
elsif files
zipline(files, filename)
end
end
def normalize(file)
file = file.file if defined?(CarrierWave::Uploader::Base) && file.is_a?(CarrierWave::Uploader::Base)
if defined?(Paperclip) && file.is_a?(Paperclip::Attachment)
if file.options[:storage] == :filesystem
{ file: File.open(file.path) }
else
{ url: file.expiring_url }
end
elsif defined?(CarrierWave::Storage::Fog::File) && file.is_a?(CarrierWave::Storage::Fog::File)
{ url: file.url }
elsif defined?(CarrierWave::SanitizedFile) && file.is_a?(CarrierWave::SanitizedFile)
{ file: File.open(file.path) }
elsif is_io?(file)
{ file: file }
elsif defined?(ActiveStorage::Blob) && file.is_a?(ActiveStorage::Blob)
{ blob: file }
elsif is_active_storage_attachment?(file) || is_active_storage_one?(file)
{ blob: file.blob }
elsif file.respond_to? :url
{ url: file.url }
elsif file.respond_to? :path
{ file: File.open(file.path) }
elsif file.respond_to? :file
{ file: File.open(file.file) }
elsif is_url?(file)
{ url: file }
else
raise(ArgumentError, 'Bad File/Stream')
end
end
I am a complete noobie to Rails and ruby. What I ultimately want is to be able to download the file from a react client using something like this
axios({
url: path,
method: 'GET',
responseType: 'blob',
}).then( async (response) => {
const fileHandle = await window.showSaveFilePicker({suggestedName: "download", types: [{accept: {"application/octet-stream":[".docx"]}}]});
const writable = await fileHandle.createWritable();
await writable.write( response.data );
await writable.close();
})
I am also not familiarized on how to work with files that good. I understand I have to use something like a writter and stream the file by chunks. I have tried some code such as
open 'large_file', 'w' do |io|
response.read_body do |chunk|
io.write chunk
end
end
and
response.read_body do |chunk|
chunk
end
But none of those have worked. If anyone could point me in the right direction or give me some ideas of what could I try in this scenario I would appreciate a lot
UPDATE
I have tried the following approach
Net::HTTP.get_response(the_remote_uri) do |response|
reader.close
open 'large_file', 'w' do |_io|
response.read_body do |chunk|
toWrite = chunk.force_encoding('UTF-8')
writer.write(toWrite)
end
end
end
But it gives me this error
<Errno::EPIPE: Broken pipe>
Something as simple as this worked for now, I dont think I am streaming the response
but still it does work, hopefully it will work with large files as well
Net::HTTP.get_response(the_remote_uri) do |resp|
self.response_body = resp.read_body
end

Lua request from Tado thermostat api

I'm building a so called 'Quickapp' in Home Center 3 (From Fibaro) in the 'Lua' programming language. I want to fetch some data from the Tado api, but it's poorly documented. I keep getting the following message from the console:
Full authentication is required to access this resourceunauthorized
I think that's because I need do assign the Bearer token from the request, but i'm a little lost how...
This is what i have so far:
function QuickApp:fetchTadoData(username,password,client_secret)
local url = "https://auth.tado.com/oauth/token"
local postdata = {
["client_id"] = "tado-web-app",
["grant_type"] = "password",
["scope"] = "home.user",
["username"] = username,
["password"] = password,
["client_secret"] = client_secret
}
local extraheaders = {
["content-type"] = "application/json"
}
self.http:request(url, {
options={
headers = extraheaders,
data = json.encode(postdata),
method = "POST"
},
success = function(status)
self:debug(status.data)
end,
error = function(error)
errorlog("Error getting data: "..error)
self:debug("hallo")
end
})
end
I know the Curl code to get the 'Bearer token' response:
curl -s "https://auth.tado.com/oauth/token" -d client_id=tado-web-app -d grant_type=password -d scope=home.user -d username="you#example.com" -d password="Password123" -d client_secret=wZa
But I don't know how to translate this to the above Lua code. Any help is appreciated!
https://manuals.fibaro.com/home-center-3-quick-apps
Looks OK, the main thing I'm noticing is this:
"self.http must have previously been created by net.HTTPClient"
function QuickApp :fetchTadoData( username, password, client_secret )
self .http = net .HTTPClient( { timeout = 5000 } ) -- 5 seconds
local url = "https://auth.tado.com/oauth/token"
local requestBody = {
action = 'create',
params = {
["client_id"] = "tado-web-app",
["grant_type"] = "password",
["scope"] = "home.user",
["username"] = username,
["password"] = password,
["client_secret"] = client_secret
}
}
local extraheaders = {
["content-type"] = "application/json",
["accept"] = "application/json"
}
self .http :request( url, {
options = {
headers = extraheaders,
data = json .encode( requestBody ),
method = "POST"
},
success = function( response )
self :debug( response .status )
self :debug( response .data )
end, -- success
error = function( msg )
self :debug( 'Error: ' ..msg )
end -- error
})
end -- :fetchTadoData()

How to use annotations to create OpenAPI (Swagger) documentation on Grails 4

We are creating API documentation for an existing Grails 4 App. We are having difficulties in understanding how to use Swagger annotations.
Let's assume the following Controller:
class IntegratorController {
def maintenanceService
def saveMaintenance() {
def message = 'success'
def status = '200'
try {
def maintenanceJson = request.JSON.maintenances
def ret=maintenanceService.processMaintenanceJSON(maintenanceJson)
} catch (Exception e) {
log.error("Error to process restricions", e)
message = 'error : ${e.getMessage()}'
status = '500'
}
def result = ['message':message]
render(status: status, contentType: "application/json", text: result as JSON)
}
}
This controller expects you to send a request JSON like this example:
{ "job":42,
"maintenances": [
{"idPort":42, "idMaintenance":42, "shipName":"myship01", "obs":"asap"},
{"idPort":43, "idMaintenance":43, "shipName":"myship02", "obs":"asap"}]}
A basic annotation will be this:
#Controller("/")
class IntegratorController {
def maintenanceService
#Post(uri="/saveMaintenance", produces = MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
#Operation(summary = "Create one or more ship maintenance")
#ApiResponse(responseCode = "500", description = "If internal service throws an Exception")
def saveMaintenance() {
def message = 'success'
def status = '200'
try {
def maintenanceJson = request.JSON.maintenances
def savedMaintenances=maintenanceService.processMaintenanceJSON(maintenanceJson)
} catch (Exception e) {
log.error("Error to process restricions", e)
message = 'error : ${e.getMessage()}'
status = '500'
}
def result = ['message':message]
render(status: status, contentType: "application/json", text: result as JSON)
}
}
Where and how to annotate the request JSON sent in the post operation?
Thank you!
The request object is "scoped" by Grails. So you need to use #RequestBody annotation to declare what it is outside the method declaration. You also need to create classes to describe what it is because the JSON deserialization is loosely typed.
This is an example:
#Post(uri="/saveMaintenance", produces = MediaType.APPLICATION_JSON)
#Operation(summary = "Summary here",
description = "Description here",
requestBody = #RequestBody(description = "Inside Operation"), tags = ["IntegratorWebController"])
#RequestBody(description = "Description here", required = true,
content = #Content(schema = #Schema(implementation = YourRequestDAO.class, anyOf = [YourRequestDAO.class, YourRequestDAODependency.class])))
#ApiResponses(value=[
#ApiResponse(responseCode="200", description = "Return status=OK in success", content = #Content(mediaType = "application/json", schema = #Schema(implementation = YourResponseDAO.class))),
#ApiResponse(responseCode="404", description = "Return status=BAD_REQUEST if you mess up", content = #Content(mediaType = "application/json", schema = #Schema(implementation = YourResponseDAO.class)))])
def saveOrUpdateActivity(){
(...)
Well Swagger and OpenAPI are 'schemas' that are preloaded at runtime to build the call structure; GraphQL also has a schema as well to load its call structure.
I did a video on it here to help you understand how this works: https://youtu.be/AJJVnwULbbc
The way Grails did this prior to 4.0 was with plugins like the 'swagger plugin' or with BeAPI plugin (which I maintain).
I don't see a supported plugin in 4.0 so I don't see how they are doing this now.

Rails download zip from binary data stream

I have a data-API to receive data from the file system. The data-API is a nodeJS server. Our webserver is a Rails server. From the webserver I send parameters to the API which files I want to download. The data-API then zips the request files and sends the zip back as binary data.
So far everything works fine. Now come the part where I'm stuck. I want to present the binary data as a download to the browser. I can either convert the binary data into a zip file and send that to the browser or have another clever solution that will present the binary data as a download.
Here's what I've got so far
Rails webserver side:
app/controllers/simulation_controller.rb
def download_multiple
files = JSON.parse(params[:files])
file_list = #simulation.sweep_points.find(params[:sweep_point_id]).file_list
send_data file_list.zip(files: files), filename: 'archive.zip', type: 'application/zip', disposition: 'attachment'
end
app/models/file_list.rb
def zip
uuid = SecureRandom.uuid
simulation = sweep_point.simulation
files = files[:files].join(" ")
url = URI.parse("http://localhost:3003/files/zip/#{simulation.name}/#{uuid}");
req = Net::HTTP::Get.new(url.to_s)
req.add_field("files", files)
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
content = res.body
content.force_encoding("ASCII-8BIT")
end
nodeJS data-API side:
exports.zip_simulation_files = (req, res) => {
const { headers, method, url } = req;
var simulation = req.params.simulation;
var uuid = req.params.uuid;
var files, command;
req.on("error", (err) => {
console.error(err);
});
files = req.headers.files;
command = "cd postprocess/bumblebee-zip " + "run" + simulation + " " + uuid + " " + "'" + files + "'";
execute(command, (data) => {
res.on("error", (err) => {
console.error(err);
});
const zipFilePath = "/home/samuel/test_bumblebee/.zips/run" + simulation + "-files-" + uuid + ".zip"
var zipFile = fs.readFileSync(zipFilePath);
var stats = fs.statSync(zipFilePath);
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-Disposition': 'attachment; filename="archive.zip"',
'Content-Length': stats.size,
'Content-Transfer-Encoding': 'binary'
});
res.end(zipFile, 'binary');
});
}
So far I'm getting back a response that is a string which seems to be a binary string. Which starts and end with this:
"PK\x03\x04\x14\x00\x00\x00\b\x00\xA8\x89\xDBJ\xB1\xB8\xA1\xBF2\x03\x00\x00\xB7F\x00\x006\x00\x1C\x00runZhangD3FINAL/13.0V/annihilation_abs_profile_001.outUT\t\x00\x0 ....... x06\x00\x00\x00\x00\x05\x00\x05\x00l\x02\x00\x00)\x10\x00\x00\x00\x00"
I've looked into different solutions in trying to turn the binary data string into a zip file or presenting the stream directly to the browser but nothing worked. Either the zip is not created or the binary was not seen as proper data.
What would be a good solution?

Downloading content from URL without prompting for authentication in Grails

I am working on a web app programmed in Grails. I have a page used to display a certain report and these reports can contain attachments. The attachment is stored in documentum and currently, when the user clicks on it, it is only a link to the location in documentum where the attachment is stored and prompts the user for his credentials. My app has documentum credentials stored in the configuration file therefore I want to use those rather than forcing the user to enter his own credentials. I am using RESTful services to retrieve link but I am trying to find a way to use the link to download directly to the users computer.
private def getFileInfo(def id, def subject) {
// product show view needs the following four lists to display the document information correctly
def ATRReportInstance = ATRReport.findByTrackingNumber(id)
def linkList = []
def nameList = []
def formatList = []
def idList = []
// open up a connection to the documentum server
def doc = connectToDocumentum()
if (!doc) return
def rest = doc.rest
def response = doc.response
if (response.status == 200) {
// retrieve the folder for this product (the name of this folder is the product's ID)
def rObjectId = rest.get(documentumServer + "/repositories/" + documentumfilestore + "?dql=select r_object_id from dm_folder where any r_folder_path='" + atrreportfolderpath + "/" + id + "'") {
auth authuser, authpass
}
// get the folder's ID from the folder object retrieved above
def folderObjectID
rObjectId.json.entries.each {
entry - >
folderObjectID = entry.content.properties.r_object_id
}
// get all of the documents in the product's MSDS folder using the folder ID retrieved above
def resp = rest.get(documentumServer + "/repositories/" + documentumfilestore + "?dql=select r_object_id, object_name, a_content_type, subject from cbs_document where any i_folder_id= '" + folderObjectID + "'") {
auth authuser, authpass
}
// cycle through the documents above to populate the four MSDS document information lists
def x = 0
resp.json.entries.each {
entry - >
if (entry.content.properties.subject == subject) {
// get the document's content object from the document's ID
def content = rest.get(documentumServer + "/repositories/" + documentumfilestore + "/objects/" + entry.content.properties.r_object_id + "/contents/content" + "?media-url-policy=local") {
auth authuser, authpass
}
if (entry.content.properties.r_object_id != null && ATRReportInstance.inactiveFiles != null && ATRReportInstance.inactiveFiles.contains(entry.content.properties.r_object_id.toString())) {} else {
linkList[x] = getLink(content.json.links, "enclosure")
if (linkList[x].contains("format=msg"))
linkList[x] = linkList[x].toString().substring(0, linkList[x].toString().indexOf("content-media")) + "content-media.msg"
formatList[x] = entry.content.properties.a_content_type
nameList[x] = entry.content.properties.object_name
idList[x] = entry.content.properties.r_object_id
x++
}
}
}
return [linkList: linkList, nameList: nameList, formatList: formatList, idList: idList]
} else {
// return null if documentum is unavailable
flash.message = message(code: 'error.documentum.unavailable')
return null
}
}
I'm thinking writing another function that can take in a URL and download the document to the user might work, but I can't figure how to retrieve that document within Grails.
If you want to bypass login you could either setup a SSO solution (requires some work for DCTM) or do a function as you suggest. However you should consider the licensing terms when doing this.
Here is the solution I implemented and that worked. It is a method that downloads a file in documentum using authentication credentials found in a configuration file.
def exportAttachment() {
//uses parameters from gsp file
def url = params.url
def name = params.name
def format = params.format
def extension
//find proper extension
for (s in documentumExtMap) {
if (s.value.equals(format)) {
extension = s.key
}
}
def connection = new URL(url).openConnection()
def remoteAuth = "Basic " + "${authuser}:${authpass}".bytes.encodeBase64()
connection.setRequestProperty("Authorization", remoteAuth)
def dataStream = connection.inputStream
response.setContentType("application/octet-stream")
response.setHeader('Content-disposition', 'Attachment; filename=' + name + '.' + extension)
response.outputStream << dataStream
response.outputStream.flush()
}
The method has three parameters: url, name, format.
Url is the location of the file in documentum.
Name is the name of the download client side
Format is the type of file that is being downloaded. In my case, I had to use this to get the proper extension needed for the file.

Resources