fix: support project folder in project settings for local file system (#559)

* fix: support project folder in project settings

* fix: support project folder validation for subfolders

Co-authored-by: alex-krasn <64093224+alex-krasn@users.noreply.github.com>
This commit is contained in:
stew-ro 2020-09-09 17:08:19 -07:00 коммит произвёл GitHub
Родитель c27a110251
Коммит b92b73bb80
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 43 добавлений и 23 удалений

Просмотреть файл

@ -328,7 +328,7 @@ export const english: IAppStrings = {
browse: "Browse",
selectFolder: "Select folder",
chooseFolder: "Choose folder",
invalidFolderMessage: "\"${project.sourceConnection.name}\" has an invalid folder. Please check it's selected folder in the Connections page",
invalidFolderMessage: "Connection [${project.sourceConnection.providerOptions.folderPath}] and project folder [${project.folderPath}] are invalid. Please check the specified folders in the Connection and Project Settings pages",
},
},
},

Просмотреть файл

@ -328,7 +328,7 @@ export const spanish: IAppStrings = {
browse: "vistazo",
selectFolder: "Seleccionar la carpeta",
chooseFolder: "Elijir la carpeta",
invalidFolderMessage: "\"${project.sourceConnection.name}\" tiene una carpeta no válida Por favor verifique su carpeta seleccionada en la página de Conexiones",
invalidFolderMessage: "La conexión [${project.sourceConnection.providerOptions.folderPath}] y la carpeta del proyecto [${project.folderPath}] no son válidas. Compruebe las carpetas especificadas en las páginas Configuración de conexión y proyecto",
},
},
},

Просмотреть файл

@ -339,7 +339,7 @@ export default class MockFactory {
public static createAssetProvider(): IAssetProvider {
return {
initialize: jest.fn(() => Promise.resolve()),
getAssets(folderPath?: string): Promise<IAsset[]> {
getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> {
throw new Error("Method not implemented.");
},
};

Просмотреть файл

@ -163,11 +163,14 @@ export default class LocalFileSystem implements IStorageProvider {
});
}
public async getAssets(folderPath?: string): Promise<IAsset[]> {
public async getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> {
folderPath = [folderPath, folderName].join("/");
const result: IAsset[] = [];
const files = await this.listFiles(path.normalize(folderPath));
for (const file of files) {
const asset = await AssetService.createAssetFromFilePath(file, undefined, true);
const fileParts = file.split(/[\\\/]/);
const fileName = fileParts[fileParts.length - 1];
const asset = await AssetService.createAssetFromFilePath(file, folderName + "/" + fileName, true);
if (this.isSupportedAssetType(asset.type)) {
const labelFileName = decodeURIComponent(`${file}${constants.labelFileExtension}`);
const ocrFileName = decodeURIComponent(`${file}${constants.ocrFileExtension}`);

Просмотреть файл

@ -12,7 +12,7 @@ import getHostProcess, { HostProcessType } from "../../common/hostProcess";
*/
export interface IAssetProvider {
initialize?(): Promise<void>;
getAssets(folderPath?: string): Promise<IAsset[]>;
getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]>;
}
/**

Просмотреть файл

@ -194,7 +194,7 @@ export class AzureBlobStorage implements IStorageProvider {
/**
* Retrieves assets from Azure Blob Storage container
*/
public async getAssets(folderPath?: string): Promise<IAsset[]> {
public async getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> {
const files: string[] = await this.listFiles(folderPath);
const result: IAsset[] = [];
for (const file of files) {

Просмотреть файл

@ -49,8 +49,12 @@ export class LocalFileSystemProxy implements IStorageProvider, IAssetProvider {
return IpcRendererProxy.send(`${PROXY_NAME}:readText`, [filePath]);
}
public isValidProjectConnection(): Promise<boolean> {
return IpcRendererProxy.send(`${PROXY_NAME}:isValidProjectConnection`, [this.options.folderPath]);
public isValidProjectConnection(fileName?: string): Promise<boolean> {
let filePath = this.options.folderPath;
if (fileName) {
filePath = [this.options.folderPath, fileName].join("/");
}
return IpcRendererProxy.send(`${PROXY_NAME}:isValidProjectConnection`, [filePath]);
}
public getFileType(fileName: string): Promise<any> {
@ -138,7 +142,6 @@ export class LocalFileSystemProxy implements IStorageProvider, IAssetProvider {
* @param folderName - Directory containing assets
*/
public getAssets(folderName?: string): Promise<IAsset[]> {
const folderPath = [this.options.folderPath, folderName].join("/");
return IpcRendererProxy.send(`${PROXY_NAME}:getAssets`, [folderPath]);
return IpcRendererProxy.send(`${PROXY_NAME}:getAssets`, [this.options.folderPath, folderName]);
}
}

Просмотреть файл

@ -93,14 +93,17 @@ export class AssetService {
if (supportedImageFormats.hasOwnProperty(assetFormat)) {
let types;
let corruptFileName;
if (nodejsMode) {
const FileType = require('file-type');
const fileType = await FileType.fromFile(normalizedPath);
types = [fileType.ext];
corruptFileName = fileName.split(/[\\\/]/).pop().replace(/%20/g, " ");
} else {
types = await this.getMimeType(filePath);
corruptFileName = fileName.split("%2F").pop().replace(/%20/g, " ");
}
const corruptFileName = fileName.split("%2F").pop().replace(/%20/g, " ");
if (!types) {
console.error(interpolate(strings.editorPage.assetWarning.incorrectFileExtension.failedToFetch, { fileName: corruptFileName.toLocaleUpperCase() }));
}
@ -217,12 +220,21 @@ export class AssetService {
public async getAssets(): Promise<IAsset[]> {
const folderPath = this.project.folderPath;
const assets = await this.assetProvider.getAssets(folderPath);
const returnedAssets = assets.map((asset) => {
asset.name = decodeURIComponent(asset.name);
return asset;
}).filter((asset) => this.isInExactFolderPath(asset.name, folderPath));
return this.filterAssets(assets, folderPath);
}
return returnedAssets;
private filterAssets = (assets, folderPath) => {
if (this.project.sourceConnection.providerType === "localFileSystemProxy") {
return assets.map((asset) => {
asset.name = decodeURIComponent(asset.name);
return asset;
})
} else {
return assets.map((asset) => {
asset.name = decodeURIComponent(asset.name);
return asset;
}).filter((asset) => this.isInExactFolderPath(asset.name, folderPath));
}
}
/**

Просмотреть файл

@ -47,11 +47,11 @@ export class OCRService {
notifyStatusChanged(OcrStatus.loadingFromAzureBlob);
ocrJson = await this.readOcrFile(ocrFileName);
if (!this.isValidOcrFormat(ocrJson) || rewrite) {
ocrJson = await this.fetchOcrUriResult(filePath, ocrFileName);
ocrJson = await this.fetchOcrUriResult(filePath, fileName, ocrFileName);
}
} catch (e) {
notifyStatusChanged(OcrStatus.runningOCR);
ocrJson = await this.fetchOcrUriResult(filePath, ocrFileName);
ocrJson = await this.fetchOcrUriResult(filePath, fileName, ocrFileName);
} finally {
notifyStatusChanged(OcrStatus.done);
}
@ -81,13 +81,11 @@ export class OCRService {
}
}
private fetchOcrUriResult = async (filePath: string, ocrFileName: string) => {
private fetchOcrUriResult = async (filePath: string, fileName: string, ocrFileName: string) => {
try {
let body;
let headers;
if (filePath.startsWith("file:")) {
const splitFilePath = filePath.split("/")
const fileName = splitFilePath[splitFilePath.length - 1];
const bodyAndType = await Promise.all(
[
this.storageProvider.readBinary(decodeURI(fileName)),

Просмотреть файл

@ -165,7 +165,11 @@ export default class ProjectService implements IProjectService {
const storageProvider = StorageProviderFactory.createFromConnection(project.sourceConnection);
let isValid;
try {
isValid = await storageProvider.isValidProjectConnection();
if (project.sourceConnection.providerType === "localFileSystemProxy") {
isValid = await storageProvider.isValidProjectConnection(project.folderPath);
} else {
isValid = await storageProvider.isValidProjectConnection();
}
} catch {
isValid = false;
}