fdarchivo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9707ba4a6f086453ad8b07e3c0a58bd46a05aac1aeee34aa1253fcec30166687
4
+ data.tar.gz: 292b3fd134b818e3ed29b99792dfb2aaadfbe5c877c40bb2de1b6b00e659c89f
5
+ SHA512:
6
+ metadata.gz: e46417b6400cf4cf6071170c779850ed341c5109f7c02b5fe32658cf4e7ab6b6002c0a8fbdee192925d2caeacae6ed59aa987c3914e739e167461808c0d92c53
7
+ data.tar.gz: 5f98abf7fcc38e969160be1c94b72055ce4e4da48828f006d02f79fbfdb44beb98c23b873880f05aca9765efa9ae479b300c1b11460e5a07b6e56bf7adb8bef1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 mtoribio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Fdarchivo
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'fdarchivo'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install fdarchivo
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Fdarchivo'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
data/lib/fdarchivo.rb ADDED
@@ -0,0 +1,111 @@
1
+ class Fdarchivo
2
+ require 'fdarchivo/fdpath'
3
+ require 'fdarchivo/fdcloud'
4
+ require 'fdarchivo/archivo'
5
+ require 'fdarchivo/configuration'
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+ attr_accessor :archivo,
10
+ :doc,
11
+ :path
12
+
13
+ attr_accessor :tmp_upload_code
14
+
15
+ public
16
+ def initialize(data)
17
+ @doc = nil
18
+ @tmp_upload_code = nil
19
+ @archivo = get_file(data)
20
+
21
+ self.path = get_path
22
+ end
23
+
24
+ def self.tmp_multi_upload(paths, owner = nil)
25
+ code = self.gen_upload_code
26
+ paths.each do |path|
27
+ a = self.new(path)
28
+ a.tmp_upload(owner, code: code)
29
+ end
30
+ code
31
+ end
32
+ def upload(owner, extra: {})
33
+ self.doc = self.create(owner: owner, extra: extra)
34
+ self.archivo = Fdcloud.new.upload(self.path, self.doc)
35
+ self.doc.update(public_url: archivo.url)
36
+ end
37
+ def tmp_upload(owner = nil, code: self.class.gen_upload_code)
38
+ self.tmp_upload_code = code
39
+ self.doc = create(owner: owner, tmp: true)
40
+ self.archivo = Fdcloud.new.tmp_upload(self.path, self.doc)
41
+ self.doc.update(public_url: archivo.url)
42
+ self.doc.upload_code
43
+ end
44
+ def create(owner: nil, tmp: false, extra: {})
45
+ Archivo.create!(
46
+ {
47
+ path: self.path,
48
+ tipo_subida: self.class,
49
+ owner: owner,
50
+ tmp: tmp,
51
+ upload_code: tmp_upload_code_to_s
52
+ }.merge(extra.to_h))
53
+ end
54
+ def reclamar
55
+ self.doc.reclamar
56
+ end
57
+
58
+ def delete
59
+ self.doc.destroy
60
+ end
61
+
62
+ def self.reclamar(ids: nil, code: nil, owner: nil, old_owner: nil)
63
+ if ids
64
+ Archivo.reclamar_by_ids(ids, owner: owner).each do |a|
65
+ self.new(a._id.to_s)
66
+ end
67
+ elsif code && owner
68
+ Archivo.reclamar(code, old_owner, owner)
69
+ end
70
+ end
71
+ def self.find_by(ids: [], code: nil, owner: nil)
72
+ Archivo.where(:$or => [
73
+ {:_id.in => ids},
74
+ {upload_code: code, owner: owner}
75
+ ])
76
+ end
77
+ def self.configure
78
+ self.configuration ||= Configuration.new
79
+ yield(configuration)
80
+ end
81
+
82
+ protected
83
+ def get_file(id)
84
+ self.doc = Archivo.find(id) if id.is_a?(String)
85
+ # Fixme: Tipo de subida
86
+ if self.doc&.tipo_subida
87
+ Fdcloud.new(self.doc.path)
88
+ else
89
+ id
90
+ end
91
+ end
92
+ def get_path
93
+ begin
94
+ self.doc&.path || self.archivo&.path
95
+ rescue
96
+ self.archivo
97
+ end
98
+ end
99
+ def self.gen_upload_code
100
+ rnd = Random.new
101
+ code = []
102
+ 6.times do
103
+ code << rnd.rand(0..9)
104
+ end
105
+ code
106
+ end
107
+
108
+ def tmp_upload_code_to_s
109
+ "#{tmp_upload_code[0]}#{tmp_upload_code[1]}#{tmp_upload_code[2]}#{tmp_upload_code[3]}#{tmp_upload_code[4]}#{tmp_upload_code[5]}" unless tmp_upload_code.nil?
110
+ end
111
+ end
@@ -0,0 +1,85 @@
1
+ class Archivo
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ TIPOS = [
6
+ :IMG, # Imagen
7
+ :DOC, # Documento
8
+ :ARC # Archivo
9
+ ]
10
+
11
+ field :path, type: String
12
+ field :public_url, type: String
13
+ field :extension, type: String
14
+ field :upload_code, type: String
15
+ field :tmp, type: Boolean, default: false
16
+ field :tipo, type: Symbol
17
+ field :tipo_subida, type: String
18
+ field :bucket, type: String
19
+ field :logo, type: Boolean, default: false
20
+
21
+ # validates_each :tipo do |doc, attr, value|
22
+ # doc.errors.add attr,
23
+ # "#{value} no esta permitido. Posibles tipos: #{TIPOS}" unless TIPOS.include?(doc.tipo)
24
+ # end
25
+ before_save do |doc|
26
+ if doc.logo
27
+ self.class.where(
28
+ :_id.ne => doc._id,
29
+ owner_id: doc.owner._id,
30
+ logo: true
31
+ ).destroy_all
32
+ end
33
+ doc.extension = doc.path.split('.').last
34
+ doc.tipo = check_tipo(doc.extension)
35
+ if doc.tmp && doc.owner.blank?
36
+ doc.path = "#{doc._id.to_s}.#{doc.extension}"
37
+ else
38
+ doc.path = "#{doc.owner.class.to_s.underscore}/#{doc.owner._id.to_s}/#{doc._id.to_s}.#{doc.extension}"
39
+ end
40
+ end
41
+
42
+ before_destroy do |doc|
43
+ Fdarchivo.new(doc._id.to_s).archivo.try(:delete)
44
+ end
45
+
46
+
47
+ belongs_to :owner,
48
+ polymorphic: true,
49
+ required: false
50
+
51
+ def self.reclamar_by_ids(ids, owner: nil)
52
+ self.where(:_id.in => ids).collect do |doc|
53
+ doc.reclamar(owner: owner)
54
+ end
55
+ end
56
+ def self.reclamar(code, old_owner, owner)
57
+ ap self.where(tmp: true, upload_code: code, owner: old_owner)
58
+ self.where(tmp: true, upload_code: code, owner: old_owner).collect do |doc|
59
+ doc.reclamar(owner: owner)
60
+ end
61
+ end
62
+
63
+ def reclamar(owner: nil)
64
+ cloud = Fdcloud.new(self.path, tmp: true)
65
+ self.update!(
66
+ tmp: false,
67
+ owner: owner
68
+ )
69
+ self.update!(
70
+ public_url: cloud.move_tmp(self).public_url
71
+ )
72
+ self
73
+ end
74
+
75
+ private
76
+ def check_tipo(ext)
77
+ case ext
78
+ when 'jpg', 'jpeg'
79
+ :IMG
80
+ else
81
+ :ARC
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,53 @@
1
+ class Configuration
2
+ require 'google/cloud'
3
+ require "google/cloud/storage"
4
+ attr_accessor :cloud_project,
5
+ :cloud_bucket,
6
+ :tmp_bucket,
7
+ :credentials_file
8
+
9
+ def initialize
10
+ @cloud_project = nil
11
+ @cloud_bucket = nil
12
+ @tmp_bucket = nil
13
+ @credentials_file = nil
14
+ end
15
+
16
+ def check
17
+ if cloud_bucket.nil? || cloud_project.nil? || credentials_file.nil?
18
+ raise "Configurarion error: #{cloud_bucket}, #{cloud_project}, #{credentials_file}, check your 'config/initializers/fdarchivo.rb'.
19
+ EJ.:
20
+ #{ejemplo_initalizer}"
21
+ else
22
+ # Iniciar el bucket principal
23
+ unless self.cloud_bucket.is_a?(Google::Cloud::Storage::Bucket)
24
+ self.cloud_bucket = Google::Cloud.new(cloud_project, credentials_file)
25
+ .storage
26
+ .bucket(cloud_bucket)
27
+ end
28
+ # Iniciar el bucket temporal
29
+ unless self.tmp_bucket.is_a?(Google::Cloud::Storage::Bucket)
30
+ self.tmp_bucket = Google::Cloud.new(cloud_project, credentials_file)
31
+ .storage
32
+ .bucket(tmp_bucket)
33
+ end
34
+ # Comprobar que el bucket principal se ha iniciado bien
35
+ unless self.cloud_bucket.is_a?(Google::Cloud::Storage::Bucket)
36
+ raise "Configuration error: Bad init cloud_bucket."
37
+ end
38
+ # Comprobar que el bucket temporal se ha iniciado bien
39
+ unless self.tmp_bucket.is_a?(Google::Cloud::Storage::Bucket)
40
+ raise "Configuration error: Bad init tmp_bucket."
41
+ end
42
+ self
43
+ end
44
+ end
45
+ private
46
+ def ejemplo_initalizer
47
+ 'Fdarchivo.configure do |config|
48
+ config.cloud_project = "name_project"
49
+ config.cloud_bucket = "name_bucket"
50
+ config.credentials_file = "#{Rails.root}/config/google.json"
51
+ end'
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ class Fdcloud < Fdarchivo
2
+ require 'google/cloud'
3
+ attr_accessor :file
4
+ #TODO: Afterdo gem check_config?
5
+ def initialize(file = nil, tmp: false)
6
+ check_config
7
+ if tmp
8
+ @file = @config.tmp_bucket.file(file) if file
9
+ else
10
+ @file = @config.cloud_bucket.file(file) if file
11
+ end
12
+ # unless file
13
+ # raise "File not found #{file}.\n Check your buckets."
14
+ # end
15
+ end
16
+
17
+ # Fixme: cambio de bucket
18
+ def change_bucket(bucket)
19
+ self.class.superclass.configure.cloud_bucket = bucket
20
+ @config = check_config
21
+ end
22
+
23
+ def upload(filepath, archivo)
24
+ self.file = @config.cloud_bucket.create_file(filepath, archivo.path)
25
+ self.file.acl.public!
26
+ # Comprobar el archivo local y borrarlo una vez subido
27
+ if File.exist?(filepath)
28
+ File.delete(filepath)
29
+ end
30
+ self
31
+ end
32
+
33
+ def tmp_upload(filepath, archivo)
34
+ if @config.tmp_bucket.nil?
35
+ raise 'Es necesario establecer tmp_bucket en el iniciador de rails.'
36
+ end
37
+ self.file = @config.tmp_bucket.create_file(filepath, archivo.path)
38
+ self.file.acl.public!
39
+ self
40
+ end
41
+
42
+ def move_tmp(archivo)
43
+ files = copy(archivo)
44
+ files[0].delete
45
+ self.file
46
+ end
47
+ def copy(archivo)
48
+ files = [self.file]
49
+ files[1] = files[0].copy(@config.cloud_bucket, archivo.path)
50
+ files[1].acl.public!
51
+ self.file = files[1]
52
+ files
53
+ end
54
+
55
+ def delete
56
+ self.file.try(:delete)
57
+ end
58
+
59
+ def url
60
+ self.file.public_url
61
+ end
62
+
63
+ def check_config
64
+ @config = self.class.superclass.configuration if self.class.superclass.configuration.check
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ class Fdpath < Fdarchivo
2
+ def initialize(path)
3
+ if File.exist?(path)
4
+ @path = File.open(path)
5
+ else
6
+ raise "File not found in #{path}"
7
+ end
8
+ end
9
+ def path
10
+ @path.path
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ class Fdarchivo
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :fdarchivo do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fdarchivo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AdrianFP
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-cloud-storage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: google-protobuf
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Description of Fdarchivo.
70
+ email:
71
+ - adrian@fuenteposadilla.es
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/fdarchivo.rb
80
+ - lib/fdarchivo/archivo.rb
81
+ - lib/fdarchivo/configuration.rb
82
+ - lib/fdarchivo/fdcloud.rb
83
+ - lib/fdarchivo/fdpath.rb
84
+ - lib/fdarchivo/version.rb
85
+ - lib/tasks/fdarchivo_tasks.rake
86
+ homepage: http://www.forumdigital.es
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.0.8
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Summary of Fdarchivo.
109
+ test_files: []