flexa_downloadable 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76e4de604a584fc382f301dc8d70d8f0ae197545
4
- data.tar.gz: a6bd4260e3628b345c22a154c345fd36f9cc34ef
3
+ metadata.gz: e43e0d4f16bfa58abb44e24f9a21f032ad26802f
4
+ data.tar.gz: 726ec92cc292b811b0320a3d24ea103816163718
5
5
  SHA512:
6
- metadata.gz: 605359049f4142aa060ac83c09c3b9fe3ae6c7d31da5ab0dfd749b3039a3b9a08af9438e8201afa5c8d09f56028f15cb8f01e52d7bb1dc92f460e62507b0fe35
7
- data.tar.gz: a6e589c655730a16153cd0fc5157cf9fef8ca29b94330f4d27ba37a602ff131d81846188e8f2eb9a74eba18882fb0ce399bf12ea6bdf5ea11fa098b9f8e973eb
6
+ metadata.gz: 008ea80636e198a2e2718e3645dc64356f94aaee0006d87a2abc073e4010a3cbd89552012b5251c14f7c362cbd04aa675986c134168cc22d9d23f792fff9f98b
7
+ data.tar.gz: 14bb1b9cc6f835222d2cd8d5cc84835cba6da5a23b87d27bdeb07548a01d91f3d14a1e7ba0ef528be0505796459d41355f1685fc9490c0b5bafbd14041010806
@@ -3,18 +3,59 @@ require_dependency "flexa_downloadable/application_controller"
3
3
  module FlexaDownloadable
4
4
  class FlexaDownloadable::DownloadsController < ApplicationController
5
5
  def download
6
+ file = find_file
7
+ exec(file)
8
+ end
9
+
10
+ def by_id
11
+ file = FlexaDownloadable::Upload.find_by(file_params)
12
+ exec(file)
13
+ end
14
+
15
+ def image
16
+ file = FlexaDownloadable::Upload.find_by(file_params)
17
+ exec(file, true)
18
+ end
19
+
20
+ def find_file
6
21
  id = params[:id].to_i
7
22
  klass = params[:klass].classify
8
23
  field = params[:field].downcase
9
24
 
10
25
  object = eval("::#{klass}.find(#{id})")
11
- file = object.send(field)
26
+ object.send(field)
27
+ end
28
+
29
+ private
30
+
31
+ def exec(file, is_image = false)
12
32
  if file
13
- send_data file.content, filename: file.filename
33
+ content = file.content
34
+ content = to_image(content) if is_image
35
+ send_data(
36
+ content,
37
+ filename: file.filename,
38
+ content_type: file.content_type,
39
+ disposition: content_disposition(is_image))
14
40
  else
15
- gflash :notice => 'Downloaded!'
16
41
  redirect_to :back
17
42
  end
18
43
  end
44
+
45
+ def content_disposition is_image
46
+ if is_image
47
+ 'inline'
48
+ else
49
+ 'attachment'
50
+ end
51
+ end
52
+
53
+ def to_image content
54
+ Image.new(content, params[:width], params[:height]).resize
55
+ end
56
+
57
+ def file_params
58
+ params.permit(:id, :filename)
59
+ end
19
60
  end
20
61
  end
@@ -1,4 +1,5 @@
1
1
  module FlexaDownloadable
2
2
  module ApplicationHelper
3
+ include DownloadHelper
3
4
  end
4
- end
5
+ end
@@ -0,0 +1,16 @@
1
+ module FlexaDownloadable
2
+ module DownloadHelper
3
+ def link_to_file(file)
4
+ link_to file.filename, file_path(file)
5
+ end
6
+
7
+ def image(file, **args)
8
+ image_tag file_path(file, args[:w], args[:h]), args
9
+ end
10
+
11
+ def file_path(file, width = nil, height = nil)
12
+ flexa_downloadable.image_path(id: file.id, filename: file.filename, \
13
+ width: width, height: height)
14
+ end
15
+ end
16
+ end
data/config/routes.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  FlexaDownloadable::Engine.routes.draw do
2
2
  get 'download/:id/:klass/:field' => 'downloads#download', as: 'flexa_download'
3
+ get 'download/:id/:filename' => 'downloads#by_id', as: 'by_id', \
4
+ constraints: { :filename => /[^\/]+/ }
5
+ get 'image/:id/:filename' => 'downloads#image', as: 'image', \
6
+ constraints: { :filename => /[^\/]+/ }
3
7
  end
@@ -7,5 +7,9 @@ module FlexaDownloadable
7
7
  mount FlexaDownloadable::Engine => "/flexa_downloadable"
8
8
  end
9
9
  end
10
+
11
+ ActiveSupport.on_load :action_controller do
12
+ helper FlexaDownloadable::ApplicationHelper
13
+ end
10
14
  end
11
15
  end
@@ -0,0 +1,19 @@
1
+ module FlexaDownloadable
2
+ class Image
3
+ def initialize image, width = nil, height = nil
4
+ @image = image
5
+ @with = width
6
+ @height = height
7
+ end
8
+
9
+ def resize
10
+ if (@width || @height)
11
+ image = MiniMagick::Image.read(@image)
12
+ resized = image.resize("#{@width}x#{@height}")
13
+ resized.to_blob
14
+ else
15
+ @image
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module FlexaDownloadable
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require "flexa_downloadable/engine"
2
+ require 'flexa_downloadable/image'
2
3
 
3
4
  module FlexaDownloadable
4
- # Your code goes here...
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexa_downloadable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gil Gomes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-24 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: mini_magick
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: sqlite3
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -54,15 +68,10 @@ files:
54
68
  - MIT-LICENSE
55
69
  - README.md
56
70
  - Rakefile
57
- - app/assets/config/flexa_downloadable_manifest.js
58
- - app/assets/javascripts/flexa_downloadable/application.js
59
- - app/assets/stylesheets/flexa_downloadable/application.css
60
71
  - app/controllers/flexa_downloadable/application_controller.rb
61
72
  - app/controllers/flexa_downloadable/downloads_controller.rb
62
73
  - app/helpers/flexa_downloadable/application_helper.rb
63
- - app/jobs/flexa_downloadable/application_job.rb
64
- - app/mailers/flexa_downloadable/application_mailer.rb
65
- - app/models/application_record.rb
74
+ - app/helpers/flexa_downloadable/download_helper.rb
66
75
  - app/models/flexa_downloadable/application_record.rb
67
76
  - app/models/flexa_downloadable/upload.rb
68
77
  - app/views/layouts/flexa_downloadable/application.html.erb
@@ -70,6 +79,7 @@ files:
70
79
  - db/migrate/20160324125419_create_flexa_downloadable_uploads.rb
71
80
  - lib/flexa_downloadable.rb
72
81
  - lib/flexa_downloadable/engine.rb
82
+ - lib/flexa_downloadable/image.rb
73
83
  - lib/flexa_downloadable/version.rb
74
84
  - lib/tasks/flexa_downloadable_tasks.rake
75
85
  homepage: https://www.flexait.com.br
@@ -92,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
102
  version: '0'
93
103
  requirements: []
94
104
  rubyforge_project:
95
- rubygems_version: 2.5.1
105
+ rubygems_version: 2.6.3
96
106
  signing_key:
97
107
  specification_version: 4
98
108
  summary: Downloadable
@@ -1,2 +0,0 @@
1
- //= link_directory ../javascripts/flexa_downloadable .js
2
- //= link_directory ../stylesheets/flexa_downloadable .css
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,4 +0,0 @@
1
- module FlexaDownloadable
2
- class ApplicationJob < ActiveJob::Base
3
- end
4
- end
@@ -1,6 +0,0 @@
1
- module FlexaDownloadable
2
- class ApplicationMailer < ActionMailer::Base
3
- default from: 'from@example.com'
4
- layout 'mailer'
5
- end
6
- end
@@ -1,3 +0,0 @@
1
- class ApplicationRecord < ActiveRecord::Base
2
- self.abstract_class = true
3
- end