Skip to content

Commit

Permalink
Revert "Pass service_name param to DirectUploadsController"
Browse files Browse the repository at this point in the history
This reverts commit 193289d.
  • Loading branch information
gmcgibbon committed Jan 28, 2022
1 parent 20846a2 commit e0714cb
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 429 deletions.
34 changes: 7 additions & 27 deletions actiontext/app/assets/javascripts/actiontext.js
Expand Up @@ -506,16 +506,14 @@ var activestorage = {exports: {}};
}
}
class BlobRecord {
constructor(file, checksum, url, directUploadToken, attachmentName) {
constructor(file, checksum, url) {
this.file = file;
this.attributes = {
filename: file.name,
content_type: file.type || "application/octet-stream",
byte_size: file.size,
checksum: checksum,
checksum: checksum
};
this.directUploadToken = directUploadToken;
this.attachmentName = attachmentName;
this.xhr = new XMLHttpRequest;
this.xhr.open("POST", url, true);
this.xhr.responseType = "json";
Expand Down Expand Up @@ -543,9 +541,7 @@ var activestorage = {exports: {}};
create(callback) {
this.callback = callback;
this.xhr.send(JSON.stringify({
blob: this.attributes,
direct_upload_token: this.directUploadToken,
attachment_name: this.attachmentName
blob: this.attributes
}));
}
requestDidLoad(event) {
Expand Down Expand Up @@ -603,12 +599,10 @@ var activestorage = {exports: {}};
}
let id = 0;
class DirectUpload {
constructor(file, url, directUploadToken, attachmentName, delegate) {
constructor(file, url, delegate) {
this.id = ++id;
this.file = file;
this.url = url;
this.directUploadToken = directUploadToken;
this.attachmentName = attachmentName;
this.delegate = delegate;
}
create(callback) {
Expand All @@ -617,7 +611,7 @@ var activestorage = {exports: {}};
callback(error);
return;
}
const blob = new BlobRecord(this.file, checksum, this.url, this.directUploadToken, this.attachmentName);
const blob = new BlobRecord(this.file, checksum, this.url);
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
blob.create((error => {
if (error) {
Expand Down Expand Up @@ -646,7 +640,7 @@ var activestorage = {exports: {}};
constructor(input, file) {
this.input = input;
this.file = file;
this.directUpload = new DirectUpload(this.file, this.url, this.directUploadToken, this.attachmentName, this);
this.directUpload = new DirectUpload(this.file, this.url, this);
this.dispatch("initialize");
}
start(callback) {
Expand Down Expand Up @@ -677,12 +671,6 @@ var activestorage = {exports: {}};
get url() {
return this.input.getAttribute("data-direct-upload-url");
}
get directUploadToken() {
return this.input.getAttribute("data-direct-upload-token");
}
get attachmentName() {
return this.input.getAttribute("data-direct-upload-attachment-name");
}
dispatch(name, detail = {}) {
detail.file = this.file;
detail.id = this.directUpload.id;
Expand Down Expand Up @@ -842,7 +830,7 @@ class AttachmentUpload {
constructor(attachment, element) {
this.attachment = attachment;
this.element = element;
this.directUpload = new activestorage.exports.DirectUpload(attachment.file, this.directUploadUrl, this.directUploadToken, this.directUploadAttachmentName, this);
this.directUpload = new activestorage.exports.DirectUpload(attachment.file, this.directUploadUrl, this);
}

start() {
Expand Down Expand Up @@ -877,14 +865,6 @@ class AttachmentUpload {
return this.element.dataset.directUploadUrl
}

get directUploadToken() {
return this.element.dataset.directUploadToken
}

get directUploadAttachmentName() {
return this.element.dataset.directUploadAttachmentName
}

get blobUrlTemplate() {
return this.element.dataset.blobUrlTemplate
}
Expand Down
8 changes: 0 additions & 8 deletions actiontext/app/helpers/action_text/tag_helper.rb
Expand Up @@ -32,14 +32,6 @@ def rich_text_area_tag(name, value = nil, options = {})
options[:data][:direct_upload_url] ||= main_app.rails_direct_uploads_url
options[:data][:blob_url_template] ||= main_app.rails_service_blob_url(":signed_id", ":filename")

class_with_attachment = "ActionText::RichText#embeds"
options[:data][:direct_upload_attachment_name] ||= class_with_attachment
options[:data][:direct_upload_token] = ActiveStorage::DirectUploadToken.generate_direct_upload_token(
class_with_attachment,
ActiveStorage::Blob.service.name,
session
)

editor_tag = content_tag("trix-editor", "", options)
input_tag = hidden_field_tag(name, value.try(:to_trix_html) || value, id: options[:input], form: form)

Expand Down
243 changes: 108 additions & 135 deletions actiontext/test/template/form_helper_test.rb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1241,7 +1241,7 @@ def hidden_field(object_name, method, options = {})
def file_field(object_name, method, options = {})
options = { include_hidden: multiple_file_field_include_hidden }.merge!(options)

Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(method, options)).render
Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(options.dup)).render
end

# Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+)
Expand Down
18 changes: 2 additions & 16 deletions actionview/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -342,7 +342,7 @@ def hidden_field_tag(name, value = nil, options = {})
# file_field_tag 'file', accept: 'text/html', class: 'upload', value: 'index.html'
# # => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
def file_field_tag(name, options = {})
text_field_tag(name, nil, convert_direct_upload_option_to_url(name, options.merge(type: :file)))
text_field_tag(name, nil, convert_direct_upload_option_to_url(options.merge(type: :file)))
end

# Creates a password field, a masked text field that will hide the users input behind a mask character.
Expand Down Expand Up @@ -984,23 +984,9 @@ def set_default_disable_with(value, tag_options)
tag_options.delete("data-disable-with")
end

def convert_direct_upload_option_to_url(name, options)
def convert_direct_upload_option_to_url(options)
if options.delete(:direct_upload) && respond_to?(:rails_direct_uploads_url)
options["data-direct-upload-url"] = rails_direct_uploads_url

if options[:object] && options[:object].class.respond_to?(:reflect_on_attachment)
attachment_reflection = options[:object].class.reflect_on_attachment(name)

class_with_attachment = "#{options[:object].class.name.underscore}##{name}"
options["data-direct-upload-attachment-name"] = class_with_attachment

service_name = attachment_reflection.options[:service_name] || ActiveStorage::Blob.service.name
options["data-direct-upload-token"] = ActiveStorage::DirectUploadToken.generate_direct_upload_token(
class_with_attachment,
service_name,
session
)
end
end
options
end
Expand Down
22 changes: 5 additions & 17 deletions activestorage/app/assets/javascripts/activestorage.esm.js
Expand Up @@ -508,16 +508,14 @@ function toArray(value) {
}

class BlobRecord {
constructor(file, checksum, url, directUploadToken, attachmentName) {
constructor(file, checksum, url) {
this.file = file;
this.attributes = {
filename: file.name,
content_type: file.type || "application/octet-stream",
byte_size: file.size,
checksum: checksum
};
this.directUploadToken = directUploadToken;
this.attachmentName = attachmentName;
this.xhr = new XMLHttpRequest;
this.xhr.open("POST", url, true);
this.xhr.responseType = "json";
Expand Down Expand Up @@ -545,9 +543,7 @@ class BlobRecord {
create(callback) {
this.callback = callback;
this.xhr.send(JSON.stringify({
blob: this.attributes,
direct_upload_token: this.directUploadToken,
attachment_name: this.attachmentName
blob: this.attributes
}));
}
requestDidLoad(event) {
Expand Down Expand Up @@ -608,12 +604,10 @@ class BlobUpload {
let id = 0;

class DirectUpload {
constructor(file, url, serviceName, attachmentName, delegate) {
constructor(file, url, delegate) {
this.id = ++id;
this.file = file;
this.url = url;
this.serviceName = serviceName;
this.attachmentName = attachmentName;
this.delegate = delegate;
}
create(callback) {
Expand All @@ -622,7 +616,7 @@ class DirectUpload {
callback(error);
return;
}
const blob = new BlobRecord(this.file, checksum, this.url, this.serviceName, this.attachmentName);
const blob = new BlobRecord(this.file, checksum, this.url);
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
blob.create((error => {
if (error) {
Expand Down Expand Up @@ -653,7 +647,7 @@ class DirectUploadController {
constructor(input, file) {
this.input = input;
this.file = file;
this.directUpload = new DirectUpload(this.file, this.url, this.directUploadToken, this.attachmentName, this);
this.directUpload = new DirectUpload(this.file, this.url, this);
this.dispatch("initialize");
}
start(callback) {
Expand Down Expand Up @@ -684,12 +678,6 @@ class DirectUploadController {
get url() {
return this.input.getAttribute("data-direct-upload-url");
}
get directUploadToken() {
return this.input.getAttribute("data-direct-upload-token");
}
get attachmentName() {
return this.input.getAttribute("data-direct-upload-attachment-name");
}
dispatch(name, detail = {}) {
detail.file = this.file;
detail.id = this.directUpload.id;
Expand Down
22 changes: 5 additions & 17 deletions activestorage/app/assets/javascripts/activestorage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -4,10 +4,8 @@
# When the client-side upload is completed, the signed_blob_id can be submitted as part of the form to reference
# the blob that was created up front.
class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController
include ActiveStorage::DirectUploadToken

def create
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args.merge(service_name: verified_service_name))
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args)
render json: direct_upload_json(blob)
end

Expand All @@ -16,10 +14,6 @@ def blob_args
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
end

def verified_service_name
ActiveStorage::DirectUploadToken.verify_direct_upload_token(params[:direct_upload_token], params[:attachment_name], session)
end

def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id).merge(direct_upload: {
url: blob.service_url_for_direct_upload,
Expand Down
13 changes: 3 additions & 10 deletions activestorage/app/javascript/activestorage/blob_record.js
@@ -1,19 +1,16 @@
import { getMetaValue } from "./helpers"

export class BlobRecord {
constructor(file, checksum, url, directUploadToken, attachmentName) {
constructor(file, checksum, url) {
this.file = file

this.attributes = {
filename: file.name,
content_type: file.type || "application/octet-stream",
byte_size: file.size,
checksum: checksum,
checksum: checksum
}

this.directUploadToken = directUploadToken
this.attachmentName = attachmentName

this.xhr = new XMLHttpRequest
this.xhr.open("POST", url, true)
this.xhr.responseType = "json"
Expand Down Expand Up @@ -46,11 +43,7 @@ export class BlobRecord {

create(callback) {
this.callback = callback
this.xhr.send(JSON.stringify({
blob: this.attributes,
direct_upload_token: this.directUploadToken,
attachment_name: this.attachmentName
}))
this.xhr.send(JSON.stringify({ blob: this.attributes }))
}

requestDidLoad(event) {
Expand Down
6 changes: 2 additions & 4 deletions activestorage/app/javascript/activestorage/direct_upload.js
Expand Up @@ -5,12 +5,10 @@ import { BlobUpload } from "./blob_upload"
let id = 0

export class DirectUpload {
constructor(file, url, serviceName, attachmentName, delegate) {
constructor(file, url, delegate) {
this.id = ++id
this.file = file
this.url = url
this.serviceName = serviceName
this.attachmentName = attachmentName
this.delegate = delegate
}

Expand All @@ -21,7 +19,7 @@ export class DirectUpload {
return
}

const blob = new BlobRecord(this.file, checksum, this.url, this.serviceName, this.attachmentName)
const blob = new BlobRecord(this.file, checksum, this.url)
notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr)

blob.create(error => {
Expand Down

0 comments on commit e0714cb

Please sign in to comment.