Skip to content

Commit

Permalink
Merge pull request #1882 from codefori/feature/saveLastLocation
Browse files Browse the repository at this point in the history
Feature/save last location
  • Loading branch information
sebjulliand authored Feb 28, 2024
2 parents 21df290 + cbc8c05 commit a9bb507
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/api/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import os from "os";
import * as vscode from 'vscode';
import { DeploymentMethod } from '../typings';
import { FilterType } from './Filter';
Expand Down Expand Up @@ -62,6 +62,7 @@ export namespace ConnectionConfiguration {
defaultDeploymentMethod: DeploymentMethod | '';
protectedPaths: string[];
showHiddenFiles: boolean;
lastDownloadLocation:string;
[name: string]: any;
}

Expand Down Expand Up @@ -143,6 +144,7 @@ export namespace ConnectionConfiguration {
defaultDeploymentMethod: parameters.defaultDeploymentMethod || ``,
protectedPaths: (parameters.protectedPaths || []),
showHiddenFiles: (parameters.showHiddenFiles === true || parameters.showHiddenFiles === undefined),
lastDownloadLocation: (parameters.lastDownloadLocation || os.homedir())
}
}

Expand Down
30 changes: 21 additions & 9 deletions src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as node_ssh from "node-ssh";
import * as vscode from "vscode";
import { ConnectionConfiguration } from "./Configuration";

import { existsSync } from "fs";
import os from "os";
import path from 'path';
import { instance } from "../instantiate";
import { CommandData, CommandResult, ConnectionData, IBMiMember, RemoteCommand } from "../typings";
Expand Down Expand Up @@ -613,7 +615,7 @@ export default class IBMi {

// Next, we're going to see if we can get the CCSID from the user or the system.
// Some things don't work without it!!!
try {
try {
const [userInfo] = await runSQL(`select CHARACTER_CODE_SET_ID from table( QSYS2.QSYUSRINFO( USERNAME => upper('${this.currentUser}') ) )`);
if (userInfo.CHARACTER_CODE_SET_ID !== `null` && typeof userInfo.CHARACTER_CODE_SET_ID === 'number') {
this.qccsid = userInfo.CHARACTER_CODE_SET_ID;
Expand Down Expand Up @@ -676,7 +678,7 @@ export default class IBMi {
}
}

if((this.qccsid < 1 || this.qccsid === 65535)){
if ((this.qccsid < 1 || this.qccsid === 65535)) {
this.outputChannel?.appendLine(`\nUser CCSID is ${this.qccsid}; falling back to using default CCSID ${this.defaultCCSID}\n`);
}

Expand Down Expand Up @@ -1145,15 +1147,25 @@ export default class IBMi {
await this.client.getDirectory(this.fileToPath(localDirectory), remoteDirectory, options);
}

getLastDownloadLocation() {
if(this.config?.lastDownloadLocation && existsSync(Tools.fixWindowsPath(this.config.lastDownloadLocation))){
return this.config.lastDownloadLocation;
}
else{
return os.homedir();
}
}

async setLastDownloadLocation(location: string) {
if (this.config && location && location !== this.config.lastDownloadLocation) {
this.config.lastDownloadLocation = location;
await ConnectionConfiguration.update(this.config);
}
}

fileToPath(file: string | vscode.Uri): string {
if (typeof file === "string") {
if (process.platform === `win32` && file[0] === `/`) {
//Issue with getFile not working propertly on Windows
//when there was a / at the start.
return file.substring(1);
} else {
return file;
}
return Tools.fixWindowsPath(file);
}
else {
return file.fsPath;
Expand Down
14 changes: 12 additions & 2 deletions src/api/Tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Crypto from 'crypto';
import { readFileSync } from "fs";
import path from "path";
import vscode from "vscode";
import { t } from "../locale";
import { IBMiMessage, IBMiMessages, QsysPath } from '../typings';
import { API, GitExtension } from "./import/git";
import { t } from "../locale";

export namespace Tools {
export class SqlError extends Error {
Expand Down Expand Up @@ -304,10 +304,20 @@ export namespace Tools {
).join("\n");
}

export function generateTooltipHtmlTable(header:string, rows: Record<string, any>){
export function generateTooltipHtmlTable(header: string, rows: Record<string, any>) {
return `<table>`
.concat(`${header ? `<thead>${header}</thead>` : ``}`)
.concat(`${Object.entries(rows).map(([key, value]) => `<tr><td>${t(key)}:</td><td>&nbsp;${value}</td></tr>`).join(``)}`)
.concat(`</table>`);
}

export function fixWindowsPath(path:string){
if (process.platform === `win32` && path[0] === `/`) {
//Issue with getFile not working propertly on Windows
//when there was a / at the start.
return path.substring(1);
} else {
return path;
}
}
}
1 change: 0 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>
console.log(`Congratulations, your extension "code-for-ibmi" is now active!`);

await loadAllofExtension(context);

const checkLastConnections = () => {
const connections = (GlobalConfiguration.get<ConnectionData[]>(`connections`) || []);
const lastConnections = (GlobalStorage.get().getLastConnections() || []).filter(lc => connections.find(c => c.name === lc.name));
Expand Down
6 changes: 3 additions & 3 deletions src/views/ifsBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from "os";
import path from "path";
import path, { dirname } from "path";
import vscode, { FileType } from "vscode";

import { ConnectionConfiguration, GlobalConfiguration } from "../api/Configuration";
Expand Down Expand Up @@ -733,10 +733,10 @@ export function initializeIFSBrowser(context: vscode.ExtensionContext) {
const ibmi = instance.getConnection();
if (ibmi) {
//Get filename from path on server
const remoteFilepath = path.join(os.homedir(), path.basename(node.path));

const remoteFilepath = path.join(ibmi.getLastDownloadLocation(), path.basename(node.path));
const localPath = (await vscode.window.showSaveDialog({ defaultUri: vscode.Uri.file(remoteFilepath) }))?.path;
if (localPath) {
await ibmi.setLastDownloadLocation(dirname(localPath));
try {
await ibmi.downloadFile(localPath, node.path);
vscode.window.showInformationMessage(t(`ifsBrowser.downloadStreamfile.infoMessage`));
Expand Down
16 changes: 4 additions & 12 deletions src/views/objectBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "fs";
import os from "os";
import path, { dirname } from "path";
import util from "util";
import vscode from "vscode";
import { ConnectionConfiguration, DefaultOpenMode, GlobalConfiguration } from "../api/Configuration";
Expand Down Expand Up @@ -796,20 +797,11 @@ export function initializeObjectBrowser(context: vscode.ExtensionContext) {

const memberContent = await contentApi.downloadMemberContent(asp, library, file, member);

let localFilepath = await vscode.window.showSaveDialog({ defaultUri: vscode.Uri.file(os.homedir() + `/` + basename) });

const localFilepath = (await vscode.window.showSaveDialog({ defaultUri: vscode.Uri.file(path.join(connection.getLastDownloadLocation(), basename)) }))?.path;
if (localFilepath) {
let localPath = localFilepath.path;
if (process.platform === `win32`) {
//Issue with getFile not working properly on Windows
//when there is a / at the start.
if (localPath[0] === `/`) {
localPath = localPath.substring(1);
}
}

await connection.setLastDownloadLocation(dirname(localFilepath));
try {
await writeFileAsync(localPath, memberContent, `utf8`);
await writeFileAsync(Tools.fixWindowsPath(localFilepath), memberContent, `utf8`);
vscode.window.showInformationMessage(t(`objectBrowser.downloadMemberContent.infoMessage`));
} catch (e) {
vscode.window.showErrorMessage(t(`objectBrowser.downloadMemberContent.errorMessage`, e));
Expand Down

0 comments on commit a9bb507

Please sign in to comment.