This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.0.9: Added (undocumented) support for 'file' input type
- Added a new expectedInput type, 'file' - Enables devs to specify that (API and UI) clients should supply a file input for the job. The UI renders this as an <input type="file" /> - Using a file input in a template expression (e.g. '${inputs.someFile}') automatically coerces the user-supplied file into a local filepath that your application can read. - This is effectively sugar for '${toFile(inputs.foo)}', but with some extra quality-of-life benefits. - The main QOL benefit is that Jobson will try to leave the user-supplied filename untouched, which is useful if your application relies on extension matching - This feature is currently undocumented due to time constraints. The final API of the released version *should* (famous last words) be identical.
- Loading branch information
Showing
30 changed files
with
614 additions
and
42 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
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
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
127 changes: 127 additions & 0 deletions
127
jobson-ui/src/ts/components/inputeditors/FileInputEditor.tsx
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,127 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {Component, ReactElement} from "react"; | ||
import {InputEditorProps} from "./InputEditor"; | ||
import * as React from "react"; | ||
import {InputEditorUpdate} from "./updates/InputEditorUpdate"; | ||
|
||
const enum States { | ||
Showing, | ||
Loading, | ||
} | ||
|
||
interface ShowingState { | ||
type: States.Showing; | ||
error: string | null; | ||
} | ||
|
||
interface LoadingState { | ||
type: States.Loading; | ||
} | ||
|
||
type State = ShowingState | LoadingState; | ||
|
||
function toB64(file: File): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = e => { | ||
const contents = e.target.result as ArrayBuffer; | ||
const b64 = btoa(String.fromCharCode.apply(null, new Uint8Array(contents))); | ||
resolve(b64); | ||
}; | ||
reader.onerror = e => { | ||
reject(e); | ||
}; | ||
reader.onabort = e => { | ||
reject(e); | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
|
||
export class FileInputEditor extends Component<InputEditorProps, State> { | ||
|
||
public state: State = { | ||
type: States.Showing, | ||
error: null, | ||
}; | ||
|
||
private fileInput: React.RefObject<HTMLInputElement> = React.createRef(); | ||
|
||
public render(): ReactElement<any> { | ||
switch (this.state.type) { | ||
case States.Showing: | ||
return this.renderShowingState(); | ||
case States.Loading: | ||
return this.renderLoadingState(); | ||
} | ||
} | ||
|
||
private renderShowingState(): ReactElement<any> { | ||
return ( | ||
<div> | ||
<input type="file" | ||
ref={this.fileInput} | ||
onChange={e => this.onFileInputChanged(e)} | ||
required /> | ||
</div> | ||
); | ||
} | ||
|
||
private renderLoadingState(): ReactElement<any> { | ||
return ( | ||
<div> | ||
<input type="file" | ||
ref={this.fileInput} | ||
onChange={e => this.onFileInputChanged(e)} | ||
required /> | ||
Loading... | ||
</div> | ||
); | ||
} | ||
|
||
private onFileInputChanged(e: React.FormEvent): void { | ||
const file: File = this.fileInput.current.files[0]; | ||
const filename = file.name; | ||
|
||
this.setState({ | ||
type: States.Loading, | ||
}, () => { | ||
toB64(file) | ||
.then(b64Str => { | ||
const upd = InputEditorUpdate.value({ | ||
filename: file.name, | ||
data: b64Str, | ||
}); | ||
this.props.onJobInputUpdate(upd); | ||
this.setState({ | ||
type: States.Showing, | ||
error: null, | ||
}); | ||
}) | ||
.catch(err => { | ||
this.setState({ | ||
type: States.Showing, | ||
error: err, | ||
}); | ||
}) | ||
}); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
jobson/src/main/java/com/github/jobson/jobinputs/file/FileExpectedInput.java
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,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.github.jobson.jobinputs.file; | ||
|
||
import com.github.jobson.jobinputs.JobExpectedInput; | ||
import com.github.jobson.utils.ValidationError; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public final class FileExpectedInput extends JobExpectedInput<FileInput> { | ||
|
||
@Override | ||
public Class<FileInput> getExpectedInputClass() { | ||
return FileInput.class; | ||
} | ||
|
||
@Override | ||
public Optional<List<ValidationError>> validate(FileInput input) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public FileInput generateExampleInput() { | ||
// A file containing "Hello, world!" as a b64-encoded string | ||
return new FileInput("hello-world.txt", "SGVsbG8sIHdvcmxkIQo="); | ||
} | ||
} |
Oops, something went wrong.