From 9d7185d385b732ad1542334761794690ae16ed19 Mon Sep 17 00:00:00 2001
From: nar20 <nar20@inf.ufpr.br>
Date: Mon, 27 May 2024 12:00:41 -0300
Subject: [PATCH 1/2] Issue #61: WIP create download route

---
 .../concerns/downloadable_controller.rb       | 30 ++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/app/controllers/concerns/downloadable_controller.rb b/app/controllers/concerns/downloadable_controller.rb
index fd409412..b001a804 100644
--- a/app/controllers/concerns/downloadable_controller.rb
+++ b/app/controllers/concerns/downloadable_controller.rb
@@ -22,14 +22,30 @@ module DownloadableController
 
   # GET /learning_objects/1/download
   def download
+    
+    Rails.logger.info "\n\nSo far... \nSo good...\n\n"
+    
+    object_id = params.values[2]
+    Rails.logger.info "This is my learning_object id: #{object_id}\n\n"
+    
+    
+    
+    # how it used to be (not working) #
     # Change request.remote_ip to req.env["HTTP_X_REAL_IP"] in production
-    downloadable.download(current_user, request.remote_ip)
-    link = downloadable.download_link
-    if !link.blank?
-      redirect_to link
-    else
-      render status: :internal_server_error
-    end
+    # downloadable.download(current_user, request.remote_ip)
+    # link = downloadable.download_link
+    # if !link.blank?
+    #   redirect_to link
+    # else
+    #   render status: :internal_server_error
+    # end
+    
+
+
+    # an idea on how to proceed #
+    # extension= #get file extension
+    # send_file Rails.root.join('public', 'uploads', #file),
+    # :type=>"aplicattion/#{extension[1]}", :x_sendfile=>true
   end
 
   protected
-- 
GitLab


From a4cedb76d49cfcbe0668bd618625e83e14ed48f2 Mon Sep 17 00:00:00 2001
From: nar20 <nar20@inf.ufpr.br>
Date: Wed, 3 Jul 2024 12:03:17 -0300
Subject: [PATCH 2/2] [add] new download route

---
 .../concerns/downloadable_controller.rb       | 33 ++++++++-----------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/app/controllers/concerns/downloadable_controller.rb b/app/controllers/concerns/downloadable_controller.rb
index b001a804..e7e872cf 100644
--- a/app/controllers/concerns/downloadable_controller.rb
+++ b/app/controllers/concerns/downloadable_controller.rb
@@ -22,30 +22,25 @@ module DownloadableController
 
   # GET /learning_objects/1/download
   def download
-    
-    Rails.logger.info "\n\nSo far... \nSo good...\n\n"
-    
     object_id = params.values[2]
-    Rails.logger.info "This is my learning_object id: #{object_id}\n\n"
-    
     
+    object = LearningObject.where(id: object_id).first
+    attachment_id = object.attachment_id
+
+    attachment = LearningObject::Attachment.where(id: attachment_id).first.as_json
     
-    # how it used to be (not working) #
-    # Change request.remote_ip to req.env["HTTP_X_REAL_IP"] in production
-    # downloadable.download(current_user, request.remote_ip)
-    # link = downloadable.download_link
-    # if !link.blank?
-    #   redirect_to link
-    # else
-    #   render status: :internal_server_error
-    # end
-    
+    attachment_name = attachment["name"]
 
+    link = attachment["retrieve_link"]
+    source = "https://mecdb4.c3sl.ufpr.br:8443" + link
+    
+    require 'open-uri'
+   
+    data = URI.open(source)
+    send_data data, :disposition => 'attachment', :filename=>attachment_name
 
-    # an idea on how to proceed #
-    # extension= #get file extension
-    # send_file Rails.root.join('public', 'uploads', #file),
-    # :type=>"aplicattion/#{extension[1]}", :x_sendfile=>true
+    # https://mecdb4.c3sl.ufpr.br:8443/rest/bitstreams/11104/retrieve
+    # https://api.portalmec.c3sl.ufpr.br/v1/learning_objects/10943/download
   end
 
   protected
-- 
GitLab