Skip to content

Commit

Permalink
feat(bucketExplorer): copy object
Browse files Browse the repository at this point in the history
  • Loading branch information
fangbinwei committed Jun 20, 2020
1 parent b2f2d65 commit acea402
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"light": "resources/light/link.svg",
"dark": "resources/dark/link.svg"
}
},
{
"command": "elan.bucketExplorer.copyFromContext",
"title": "Copy to"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -119,6 +123,10 @@
{
"command": "elan.bucketExplorer.copyLink",
"when": "false"
},
{
"command": "elan.bucketExplorer.copyFromContext",
"when": "false"
}
],
"explorer/context": [
Expand Down Expand Up @@ -149,11 +157,16 @@
{
"command": "elan.bucketExplorer.uploadFromContext",
"when": "view == bucketExplorer && viewItem == folder || view == bucketExplorer &&viewItem == bucket",
"group": "5_cutcopypaste"
"group": "upload@1"
},
{
"command": "elan.bucketExplorer.uploadFromClipboard",
"when": "view == bucketExplorer && viewItem == folder || view == bucketExplorer &&viewItem == bucket",
"group": "upload@1"
},
{
"command": "elan.bucketExplorer.copyFromContext",
"when": "view == bucketExplorer && viewItem == object",
"group": "5_cutcopypaste"
}
]
Expand Down
36 changes: 36 additions & 0 deletions src/commands/bucketExplorer/copyFromContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import vscode from 'vscode'
import { OSSObjectTreeItem } from '@/views/bucket'
import { ext } from '@/extensionVariables'
import { CommandContext } from '@/constant'
import { copyUri } from '@/uploader/copyUri'
import Logger from '@/utils/log'
import { removeLeadingSlash } from '@/utils'

async function copyFromBucketExplorerContext(
treeItem: OSSObjectTreeItem
): Promise<void> {
const sourceUri = vscode.Uri.parse(treeItem.url)
const targetName = await vscode.window.showInputBox({
value: removeLeadingSlash(sourceUri.path),
placeHolder: `Enter target name. e.g., 'example/folder/name/target.jpg'`,
validateInput: (text) => {
text = text.trim()
if (text[0] === '/') return `Please do not start with '/'.`
if (text === '') return `Please enter target name.`
}
})
if (!targetName) return
try {
await copyUri(vscode.Uri.file(targetName), sourceUri)
} catch {
Logger.log('catch function copyUri error')
}

ext.bucketExplorer.refresh()
}
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace copyFromBucketExplorerContext {
export const command = CommandContext.BUCKET_EXPLORER_COPY_CONTEXT
}

export { copyFromBucketExplorerContext }
1 change: 1 addition & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum CommandContext {
BUCKET_EXPLORER_UPLOAD_CLIPBOARD = 'elan.bucketExplorer.uploadFromClipboard',
BUCKET_EXPLORER_UPLOAD_CONTEXT = 'elan.bucketExplorer.uploadFromContext',
BUCKET_EXPLORER_DELETE_CONTEXT = 'elan.bucketExplorer.deleteFromContext',
BUCKET_EXPLORER_COPY_CONTEXT = 'elan.bucketExplorer.copyFromContext',
BUCKET_EXPLORER_REFRESH_ROOT = 'elan.bucketExplorer.refreshRoot',
BUCKET_EXPLORER_COPY_LINK = 'elan.bucketExplorer.copyLink'
}
Expand Down
51 changes: 51 additions & 0 deletions src/uploader/copyUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Uploader from './index'
import { getProgress, removeLeadingSlash, Progress } from '@/utils'
import vscode from 'vscode'
import Logger from '@/utils/log'

export async function copyUri(
targetUri: vscode.Uri,
sourceUri: vscode.Uri,
showProgress = true
): Promise<void> {
const uploader = Uploader.get()
// init OSS instance failed
if (!uploader) return

// path '/ex/path', the 'ex' means source bucket name, should remove leading slash
const sourceName = removeLeadingSlash(sourceUri.path)
// leading slash of targetName is irrelevant
const targetName = removeLeadingSlash(targetUri.path)

let progress: Progress['progress'] | undefined
let progressResolve: Progress['progressResolve'] | undefined
if (showProgress) {
const p = getProgress(`Copying image`)
progress = p.progress
progressResolve = p.progressResolve
}
try {
await uploader.copy(targetName, sourceName)
if (progress && progressResolve) {
progress.report({
message: `Finish.`,
increment: 100
})
;((fn): void => {
setTimeout(() => {
fn()
}, 1000)
})(progressResolve)
}
} catch (err) {
progressResolve && progressResolve()
Logger.showErrorMessage(
`Failed to copy image. See output channel for more details`
)
Logger.log(
`Failed: copy from ${sourceName} to ${targetName}.` +
` Reason: ${err.message}`
)
throw err
}
}
9 changes: 9 additions & 0 deletions src/uploader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@ export default class Uploader {
query = Object.assign(defaultConfig, query)
return this.client.list(query, options)
}

async copy(
name: string,
sourceName: string,
sourceBucket?: string,
options?: OSS.CopyObjectOptions
): Promise<OSS.CopyAndPutMetaResult> {
return this.client.copy(name, sourceName, sourceBucket, options)
}
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function getOSSConfiguration(): OSS.Options {
}
}

interface Progress {
export interface Progress {
progress: vscode.Progress<{ message?: string; increment?: number }>
progressResolve: (value?: unknown) => void
progressReject: (value?: unknown) => void
Expand Down
5 changes: 5 additions & 0 deletions src/views/registerBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { deleteFromBucketExplorerContext } from '@/commands/bucketExplorer/delet
import { uploadFromBucketExplorerContext } from '@/commands/bucketExplorer/uploadFromContext'
import { uploadFromBucketExplorerClipboard } from '@/commands/bucketExplorer/uploadFromClipboard'
import { copyLinkFromBucketExplorer } from '@/commands/bucketExplorer/copyLink'
import { copyFromBucketExplorerContext } from '@/commands/bucketExplorer/copyFromContext'
import { CommandContext } from '@/constant'

export function registerBucket(context: vscode.ExtensionContext): void {
Expand Down Expand Up @@ -33,6 +34,10 @@ export function registerBucket(context: vscode.ExtensionContext): void {
vscode.commands.registerCommand(
copyLinkFromBucketExplorer.command,
copyLinkFromBucketExplorer
),
vscode.commands.registerCommand(
copyFromBucketExplorerContext.command,
copyFromBucketExplorerContext
)
]
context.subscriptions.push(ext.bucketExplorerTreeView)
Expand Down

0 comments on commit acea402

Please sign in to comment.