Skip to content

Commit

Permalink
Code refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
pszczesniak committed Jun 6, 2023
1 parent 539a6cf commit 4db90dd
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/app/plugins/githubuploadadapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,19 @@ export class Adapter {
// Step 2: the real upload takes place this time to Amazon S3 servers,
// using information returned from Step 1.

// The final URL of the file is already known, even before the upload. We save it here.
returnUrl = response.asset.href;

/**
* This is a copy&paste code from gitHub source code `attachment-upload.ts`
*
* It marks uploaded file as completed and return correct URL.
*/
const url = typeof response.asset_upload_url === 'string' ? response.asset_upload_url : null;
const token = typeof response.asset_upload_authenticity_token == 'string' ?
const assetUrl = typeof response.asset_upload_url === 'string' ? response.asset_upload_url : null;
const authenticityToken = typeof response.asset_upload_authenticity_token == 'string' ?
response.asset_upload_authenticity_token : null;

if ( !( url && token ) ) {
if ( !( assetUrl && authenticityToken ) ) {
return;
}

const form = new FormData();
form.append( 'authenticity_token', token );

fetch( url, {
form.append( 'authenticity_token', authenticityToken );

fetch( assetUrl, {
method: 'PUT',
body: form,
credentials: 'same-origin',
Expand All @@ -133,11 +126,10 @@ export class Adapter {
} )
.then( response => response.json() )
.then( response => {
// The final URL of the file is already known, even before the upload. We save it here.
returnUrl = response.href;
} );

/* End of copied GitHub code */

// Retrieve the target Amazon S3 upload URL.
const uploadUrl = response.upload_url;

Expand Down

0 comments on commit 4db90dd

Please sign in to comment.