import React from "react"; import { observable, action, computed, makeObservable } from "mobx"; import { observer } from "mobx-react"; import { getFileName, getValidFileNameFromFileName, getShortFileName, fileExists, isValidFileName, isValidPath } from "eez-studio-shared/util-electron"; import { makeValidator, validators } from "eez-studio-shared/validation"; import { Dialog, showDialog } from "eez-studio-ui/dialog"; import { PropertyList, TextInputProperty, NumberInputProperty, BooleanProperty } from "eez-studio-ui/properties"; import { FileInputProperty } from "eez-studio-ui/properties-electron"; import { IFileUploadInstructions } from "instrument/connection/file-upload"; const FileUploadSettingsDialog = observer( class FileUploadSettingsDialog extends React.Component<{ instructions: IFileUploadInstructions; callback: (instructions: IFileUploadInstructions) => void; }> { constructor(props: any) { super(props); makeObservable(this, { instructions: observable }); this.instructions = { ...this.props.instructions }; } instructions: IFileUploadInstructions; validator = makeValidator({ startCommandTemplate: validators.required, sendChunkCommandTemplate: validators.required, chunkSize: validators.rangeInclusive(1, 4096) }); handleSubmit = async () => { if (!(await this.validator.checkValidity(this.instructions))) { return false; } this.props.callback(this.instructions); return true; }; render() { return ( (this.instructions.shortFileName = value) )} /> (this.instructions.startCommandTemplate = value) )} errors={this.validator.errors.startCommandTemplate} /> (this.instructions.fileSizeCommandTemplate = value) )} /> (this.instructions.sendChunkCommandTemplate = value) )} errors={ this.validator.errors.sendChunkCommandTemplate } /> (this.instructions.finishCommandTemplate = value) )} /> (this.instructions.abortCommandTemplate = value) )} /> (this.instructions.chunkSize = value) )} errors={this.validator.errors.chunkSize} /> ); } } ); function showAdvancedSettingsDialog( instructions: IFileUploadInstructions, callback: (instructions: IFileUploadInstructions) => void ) { showDialog( ); } const FileUploadDialog = observer( class FileUploadDialog extends React.Component<{ instructions: IFileUploadInstructions; callback: (instructions: IFileUploadInstructions) => void; }> { constructor(props: any) { super(props); makeObservable(this, { instructions: observable, destinationFileNameChanged: observable, destinationFolderPathChanged: observable, destinationFoldePathSuggestions: computed }); this.instructions = { ...this.props.instructions }; } instructions: IFileUploadInstructions; destinationFileNameChanged = false; destinationFolderPathChanged = false; validator = makeValidator({ sourceFilePath: [ validators.required, async () => { if ( this.instructions.sourceFilePath && !(await fileExists(this.instructions.sourceFilePath)) ) { return "File not found."; } return null; } ], destinationFileName: [ validators.required, () => { if ( this.instructions.destinationFileName && !isValidFileName( this.instructions.destinationFileName, this.instructions.shortFileName ) ) { return "Invalid file name."; } return null; } ], destinationFolderPath: [ () => { if ( this.instructions.destinationFolderPath && !isValidPath( this.instructions.destinationFolderPath, this.instructions.shortFileName ) ) { return "Invalid path."; } return null; } ] }); handleSubmit = async () => { if (!(await this.validator.checkValidity(this.instructions))) { return false; } this.props.callback(this.instructions); return true; }; deriveDestinationFileNameFromSourceFilePath() { if (this.instructions.shortFileName) { return getShortFileName(this.instructions.sourceFilePath!); } else { return getValidFileNameFromFileName( getFileName(this.instructions.sourceFilePath!) ); } } deriveDestinationFolderPathFromSourceFilePath() { const sourceFilePath = this.instructions.sourceFilePath; const favoriteDestinationPaths = this.instructions.favoriteDestinationPaths; if (sourceFilePath && favoriteDestinationPaths) { const favoriteDestinationPath = favoriteDestinationPaths.find( favoriteDestinationPath => favoriteDestinationPath.ext && sourceFilePath!.endsWith(favoriteDestinationPath.ext) ); if (favoriteDestinationPath) { return favoriteDestinationPath.path; } } return this.instructions.destinationFolderPath; } get destinationFoldePathSuggestions() { if (this.instructions.favoriteDestinationPaths) { return this.instructions.favoriteDestinationPaths .map( favoriteDestinationPath => favoriteDestinationPath.path ) .filter( (favoriteDestinationPath, index, self) => self.indexOf(favoriteDestinationPath) == index ); } return undefined; } render() { return ( showAdvancedSettingsDialog( this.instructions, action( ( instructions: IFileUploadInstructions ) => { let updateDestinationFileName = this.instructions .destinationFileName === this.deriveDestinationFileNameFromSourceFilePath(); Object.assign( this.instructions, instructions ); if (updateDestinationFileName) { this.instructions.destinationFileName = this.deriveDestinationFileNameFromSourceFilePath(); } } ) ), disabled: false, style: { marginRight: "auto" }, icon: "material:settings", title: "Show advanced settings" } ]} > { this.instructions.sourceFilePath = value; let destinationFileName = this.instructions.destinationFileName; if (destinationFileName) { destinationFileName = destinationFileName.trim(); } if ( !destinationFileName || !this.destinationFileNameChanged ) { this.instructions.destinationFileName = this.deriveDestinationFileNameFromSourceFilePath(); } let destinationFolderPath = this.instructions.destinationFolderPath; if (destinationFolderPath) { destinationFolderPath = destinationFolderPath.trim(); } if ( !destinationFolderPath || !this.destinationFolderPathChanged ) { this.instructions.destinationFolderPath = this.deriveDestinationFolderPathFromSourceFilePath(); } })} errors={this.validator.errors.sourceFilePath} /> { this.instructions.destinationFileName = value; this.destinationFileNameChanged = true; })} errors={this.validator.errors.destinationFileName} /> { this.instructions.destinationFolderPath = value; this.destinationFolderPathChanged = true; })} errors={this.validator.errors.destinationFolderPath} suggestions={this.destinationFoldePathSuggestions} /> ); } } ); export function showFileUploadDialog( instructions: IFileUploadInstructions, callback: (instructions: IFileUploadInstructions) => void ) { showDialog( ); }