-
-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Website demo – support HTTP uploads #1024
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { on } from '@ember/modifier'; | ||
import { htmlSafe } from '@ember/template'; | ||
|
||
export const UPLOAD_TYPES = { | ||
simulated: 'Simulated 🧑🔬', | ||
http: 'HTTP 📡', | ||
}; | ||
|
||
// Simulated | ||
// Values in kilobits per second (kbps) | ||
const RATES = { | ||
'Disconnected - 0 Mbps': 0, | ||
'Very slow - 0.1 Mbps': 100, | ||
'Slow 3G - 0.4 Mbps': 400, | ||
'Fast 3G - 0.675 Mbps': 675, | ||
'ADSL - 1.5 Mbps': 1_500, | ||
'4G/LTE - 50 Mbps': 50_000, | ||
'Fast Fibre - 100 Mbps': 100_000, | ||
}; | ||
const DEFAULT_RATE = RATES['Fast 3G - 0.675 Mbps']; | ||
|
||
// HTTP | ||
const DEFAULT_URL = 'https://api.bytescale.com/v1/files/basic'; | ||
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD']; | ||
const DEFAULT_METHOD = 'POST'; | ||
const DEFAULT_HEADERS = { | ||
Authorization: 'Basic YXBpa2V5OmZyZWU', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}; | ||
|
||
export const DEFAULT_OPTIONS = { | ||
type: UPLOAD_TYPES.simulated, | ||
// Simulated | ||
rate: DEFAULT_RATE, | ||
// HTTP | ||
url: DEFAULT_URL, | ||
method: DEFAULT_METHOD, | ||
headers: DEFAULT_HEADERS, | ||
}; | ||
|
||
const eq = (a, b) => a === b; | ||
|
||
export default class OptionsFormComponent extends Component { | ||
@action | ||
setOptions(event) { | ||
const formData = new FormData(event.currentTarget); | ||
const entries = Object.fromEntries(formData.entries()); | ||
const uploadOptions = { | ||
...entries, | ||
rate: parseInt(entries.rate, 10), | ||
headers: JSON.parse(entries.headers), | ||
}; | ||
this.args.onUpdate(uploadOptions); | ||
} | ||
|
||
// Keep forms in DOM to prevent data loss | ||
toggleVisibility = (type) => { | ||
return this.args.uploadOptions.type === type | ||
? htmlSafe('') | ||
: htmlSafe('display: none'); | ||
}; | ||
|
||
<template> | ||
<form aria-label='Upload options' {{on 'change' this.setOptions}}> | ||
<fieldset> | ||
<legend>Upload type:</legend> | ||
{{#each (Object.values UPLOAD_TYPES) as |type|}} | ||
<div> | ||
<input | ||
type='radio' | ||
name='type' | ||
id={{type}} | ||
value={{type}} | ||
checked={{eq @uploadOptions.type type}} | ||
/> | ||
<label for={{type}}>{{type}}</label> | ||
</div> | ||
{{/each}} | ||
</fieldset> | ||
|
||
<label style={{this.toggleVisibility UPLOAD_TYPES.simulated}}> | ||
Simulated speed: | ||
<select name='rate'> | ||
{{#each-in RATES as |name rate|}} | ||
<option value={{rate}} selected={{eq @uploadOptions.rate rate}}> | ||
{{name}} | ||
</option> | ||
{{/each-in}} | ||
</select> | ||
</label> | ||
|
||
<label style={{this.toggleVisibility UPLOAD_TYPES.http}}> | ||
URL: | ||
<input type='text' name='url' value={{DEFAULT_URL}} /> | ||
</label> | ||
|
||
<label style={{this.toggleVisibility UPLOAD_TYPES.http}}> | ||
Method: | ||
<select name='method'> | ||
{{#each METHODS as |method|}} | ||
<option value={{method}} selected={{eq @uploadOptions.method method}}> | ||
{{method}} | ||
</option> | ||
{{/each}} | ||
</select> | ||
</label> | ||
|
||
<label style={{this.toggleVisibility UPLOAD_TYPES.http}}> | ||
Headers: | ||
<textarea name='headers' rows='5' spellcheck='false'>{{JSON.stringify | ||
DEFAULT_HEADERS | ||
null | ||
2 | ||
}}</textarea> | ||
</label> | ||
</form> | ||
</template> | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While looking for a public "black hole" upload endpoint I found this documented in the MIT npm package
react-uploader
READMEAnd it worked for me 🙃
I also read the terms that this company publishes and there's nothing referencing public API endpoints really...
This is a suggestion that a visitor may want to use. When doing so they are using this API on their own behalf 🤷🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ended up replacing this with a WireMock endpoint from a free account I created. It doesn't behave as much like a real upload endpoint, but it does work