api-fileupload 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 27f010347cb9c6cab9371ded56a1cc6c160219aabf74e90fddbe77acfedd1917
4
+ data.tar.gz: 005fa06161d394dca3d7359c612960c372449fc4d1612647bc714718616aa460
5
+ SHA512:
6
+ metadata.gz: 9678c447046ee3d7e46c7e75955d8a485f25a5d9d184d2c17a54098a71043c99678ea1299638efd056ee85e6de45373b307ee8bb6d9da501b98c5c33cb12270d
7
+ data.tar.gz: a1ecc38e78530caf72c7cc69fc6a9549ac51a1ad68af71df30cb718eaa43bfe49b6abe629ec870052307b9307cc09fd6d005a178e357cab247477fb778672b6f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ # rspec failure tracking
17
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at balakirevs@i.ua. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Aleksandr Balakiriev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Api::Fileupload
2
+
3
+ Library for handling file uploads with json only apis.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'api-fileupload'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install api-fileupload
18
+
19
+ ## Usage
20
+
21
+ Create a controller that includes Api::Fileupload::Controller module and add
22
+ create and destroy routes for the controller.
23
+
24
+ In controllers that need those uploaded files, add
25
+ ```ruby
26
+ include Api::Fileupload::UploadedFiles
27
+ ```
28
+
29
+ and retrieve uploaded files by using
30
+ ```ruby
31
+ uploaded_file({id: <uploaded_file_id>})
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/api/fileupload/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "api-fileupload"
5
+ spec.version = Api::Fileupload::VERSION
6
+ spec.authors = ["Aleksandr Balakiriev"]
7
+ spec.email = ["info@Balakiriev.com"]
8
+
9
+ spec.summary = %q{Simple library for handling file uploads for json only apis.}
10
+ spec.description = %q{}
11
+ spec.homepage = ""
12
+ spec.license = "MIT"
13
+
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "railties", "~> 5"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "pry", "~> 0.9"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api/fileupload"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'api/fileupload/controller'
2
+ require 'api/fileupload/version'
3
+
4
+ module Api
5
+ module Fileupload
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ require 'api/fileupload/store'
2
+
3
+ module Api
4
+ module Fileupload
5
+ module Controller
6
+ def create
7
+ stored_file = Store.add(current_user.id, permitted_params[:file])
8
+ render json: stored_file, status: :created
9
+ end
10
+
11
+ def destroy
12
+ if Store.fetch(current_user.id, permitted_params[:id])
13
+ Store.remove(current_user.id, permitted_params[:id])
14
+ render nothing: true, status: :no_content
15
+ else
16
+ render nothing: true, status: :not_found
17
+ end
18
+ end
19
+
20
+ private
21
+ def permitted_params
22
+ params.permit(:file, :id)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ require 'action_dispatch'
2
+
3
+ module Api
4
+ module Fileupload
5
+ class Store
6
+ class << self
7
+ def add(key, file)
8
+ copy = copy_file(file)
9
+ id = file_id(copy)
10
+
11
+ files(key)[id] = copy
12
+ {id: id}
13
+ end
14
+
15
+ def fetch(key, id)
16
+ if id.is_a?(Hash)
17
+ files(key)[id[:id]]
18
+ else
19
+ files(key)[id]
20
+ end
21
+ end
22
+
23
+ def clear(key)
24
+ files(key).keys.each{|id| remove(key, id)}
25
+ end
26
+
27
+ def remove(key, id)
28
+ file = fetch(key, id)
29
+ files(key).delete(id)
30
+ file.tempfile.unlink
31
+ end
32
+
33
+ private
34
+ def files(key)
35
+ @files ||= {}
36
+ @files[key] ||= {}
37
+ end
38
+
39
+ def file_id(file)
40
+ file.hash.to_s
41
+ end
42
+
43
+ def copy_file(file)
44
+ unless file.is_a?(ActionDispatch::Http::UploadedFile)
45
+ throw "file #{file} has to be a ActionDispatch::Http::UploadedFile"
46
+ end
47
+
48
+ tmpfile = Tempfile.new(file.original_filename)
49
+ tmpfile.set_encoding(Encoding::BINARY) if tmpfile.respond_to?(:set_encoding)
50
+ tmpfile.binmode
51
+ FileUtils.copy(file.path, tmpfile.path)
52
+ ActionDispatch::Http::UploadedFile.new(
53
+ tempfile: tmpfile,
54
+ filename: file.original_filename,
55
+ type: file.content_type,
56
+ head: file.headers
57
+ )
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ require 'api/fileupload/store'
2
+
3
+ module Api
4
+ module Fileupload
5
+ module UploadedFiles
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ after_filter :clear_uploaded_files, only: [:create, :update], if: :response_ok?
10
+ end
11
+
12
+ protected
13
+ def uploaded_file(file)
14
+ if file.is_a?(Hash)
15
+ Store.fetch(current_user.id, file)
16
+ else
17
+ file
18
+ end
19
+ end
20
+
21
+ def uploaded_file_param(param_name = :file)
22
+ { param_name => [:id] }
23
+ end
24
+
25
+ private
26
+ def clear_uploaded_files
27
+ Store.clear(current_user.id)
28
+ end
29
+
30
+ def response_ok?
31
+ "#{response.status}" =~ /\A2/
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Api
2
+ module Fileupload
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-fileupload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksandr Balakiriev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ description: ''
84
+ email:
85
+ - info@Balakiriev.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - api-fileupload.gemspec
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/api/fileupload.rb
101
+ - lib/api/fileupload/controller.rb
102
+ - lib/api/fileupload/store.rb
103
+ - lib/api/fileupload/uploaded_files.rb
104
+ - lib/api/fileupload/version.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.8
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Simple library for handling file uploads for json only apis.
128
+ test_files: []