This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for attachments (#65)
- Loading branch information
Showing
13 changed files
with
374 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
require "rake/testtask" | ||
|
||
# rake db:create_migration NAME=... | ||
require "sinatra/activerecord/rake" | ||
|
||
Rake::TestTask.new do |t| | ||
t.pattern = "spec/*_spec.rb" | ||
end | ||
|
||
namespace :db do | ||
task :load_config do | ||
require "./lib/rubywarden.rb" | ||
end | ||
end | ||
|
||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << "spec" | ||
t.pattern = "spec/*_spec.rb" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateAttachments < ActiveRecord::Migration[5.1] | ||
def change | ||
remove_column :ciphers, :attachments | ||
create_table :attachments, id: :string, primary_key: :uuid do |t| | ||
t.string :cipher_uuid | ||
t.string :url | ||
t.string :filename | ||
t.integer :size | ||
t.binary :file | ||
t.timestamps | ||
end | ||
add_foreign_key :attachments, :ciphers, { column: :cipher_uuid, primary_key: :uuid } | ||
add_index(:attachments, :cipher_uuid) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# | ||
# Copyright (c) 2017 joshua stein <jcs@jcs.org> | ||
# | ||
# Permission to use, copy, modify, and distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
# | ||
|
||
class Attachment < DBModel | ||
self.table_name = "attachments" | ||
attr_accessor :context | ||
|
||
before_create :generate_uuid_primary_key | ||
before_create :generate_url | ||
|
||
belongs_to :cipher, foreign_key: :cipher_uuid, inverse_of: :attachments | ||
|
||
def self.build_from_params(params, context) | ||
attachment = new filename: params[:filename], | ||
size: params[:size], | ||
file: params[:file] | ||
attachment.context = context | ||
attachment | ||
end | ||
|
||
def to_hash | ||
{ | ||
"Id" => self.uuid, | ||
"Url" => self.url, | ||
"FileName" => self.filename.to_s, | ||
"Size" => self.size, | ||
"SizeName" => human_file_size, | ||
"Object" => "attachment" | ||
} | ||
end | ||
|
||
private | ||
|
||
def generate_url | ||
self.url = context.url("/attachments/#{self.cipher_uuid}/#{self.id}") | ||
end | ||
|
||
def human_file_size | ||
ActiveSupport::NumberHelper.number_to_human_size(self.size) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# Copyright (c) 2018 joshua stein <jcs@jcs.org> | ||
# | ||
# Permission to use, copy, modify, and distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
# | ||
|
||
module Rubywarden | ||
module AttachmentHelpers | ||
def retrieve_cipher(uuid: ) | ||
d = device_from_bearer | ||
if !d | ||
halt validation_error("invalid bearer") | ||
end | ||
|
||
c = nil | ||
if uuid.blank? || !(c = Cipher.find_by_user_uuid_and_uuid(d.user_uuid, uuid)) | ||
halt validation_error("invalid cipher") | ||
end | ||
return c | ||
end | ||
|
||
def delete_attachment uuid:, attachment_uuid: | ||
cipher = retrieve_cipher uuid: uuid | ||
cipher.attachments.find(attachment_uuid).destroy | ||
"" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# Copyright (c) 2018 joshua stein <jcs@jcs.org> | ||
# | ||
# Permission to use, copy, modify, and distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
# | ||
# uses helpers/attachment_helpers | ||
module Rubywarden | ||
module Routing | ||
module Attachments | ||
def self.registered(app) | ||
app.namespace BASE_URL do | ||
post "/ciphers/:uuid/attachment" do | ||
cipher = retrieve_cipher uuid: params[:uuid] | ||
|
||
need_params(:data) do |p| | ||
return validation_error("#{p} cannot be blank") | ||
end | ||
|
||
# we have to extract filename from data -> head, since data -> filename is truncated | ||
filename = nil | ||
if md = params[:data][:head].match(/filename=\"(\S+)\"\r\nContent-Type/) | ||
filename = md[1] | ||
else | ||
return validation_error("filename cannot be blank") | ||
end | ||
|
||
file = params[:data][:tempfile] | ||
attachment_params = { filename: filename, | ||
size: file.size, | ||
file: file.read } | ||
attachment = cipher.attachments.build_from_params(attachment_params, self) | ||
|
||
Attachment.transaction do | ||
if !attachment.save | ||
return validation_error("error saving") | ||
end | ||
|
||
cipher.to_hash.to_json | ||
end | ||
end | ||
|
||
delete "/ciphers/:uuid/attachment/:attachment_id" do | ||
delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id] | ||
end | ||
|
||
post "/ciphers/:uuid/attachment/:attachment_id/delete" do | ||
delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id] | ||
end | ||
end # BASE_URL | ||
|
||
app.get "/attachments/:uuid/:attachment_id" do | ||
a = Attachment.find_by_uuid_and_cipher_uuid(params[:attachment_id], params[:uuid]) | ||
attachment(a.filename) | ||
response.write(a.file) | ||
end | ||
end # registered app | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.