Skip to content

Commit 48b72ed

Browse files
committed
🐛 Fix: clipboard image upload under win10
split 2 ps1 file for win10 and under win10
1 parent 81e1df5 commit 48b72ed

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

src/utils/clipboard/windows.ps1

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
1-
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
1+
22
param($imagePath)
33

4-
# https://github.com/PowerShell/PowerShell/issues/7233
5-
# fix the output encoding bug
6-
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
4+
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
75

86
Add-Type -Assembly PresentationCore
9-
function main {
10-
$img = [Windows.Clipboard]::GetImage()
11-
12-
if ($img -eq $null) {
13-
"no image"
14-
Exit 1
15-
}
7+
$img = [Windows.Clipboard]::GetImage()
168

17-
if (-not $imagePath) {
18-
"no image"
19-
Exit 1
20-
}
21-
22-
$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
23-
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
24-
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
25-
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
26-
$encoder.Save($stream) | out-null
27-
$stream.Dispose() | out-null
28-
29-
$imagePath
9+
if ($img -eq $null) {
10+
"no image"
11+
Exit 1
3012
}
3113

32-
try {
33-
# For WIN10
34-
$file = Get-Clipboard -Format FileDropList
35-
if ($file -ne $null) {
36-
Convert-Path $file
37-
Exit 1
38-
}
39-
} catch {
40-
# For WIN7 WIN8 WIN10
41-
main
14+
if (-not $imagePath) {
15+
"no image"
16+
Exit 1
4217
}
4318

44-
main
19+
$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
20+
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
21+
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
22+
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
23+
$encoder.Save($stream) | out-null
24+
$stream.Dispose() | out-null
25+
26+
$imagePath

src/utils/clipboard/windows10.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
2+
param($imagePath)
3+
4+
# https://github.com/PowerShell/PowerShell/issues/7233
5+
# fix the output encoding bug
6+
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
7+
8+
Add-Type -Assembly PresentationCore
9+
function main {
10+
$img = [Windows.Clipboard]::GetImage()
11+
12+
if ($img -eq $null) {
13+
"no image"
14+
Exit 1
15+
}
16+
17+
if (-not $imagePath) {
18+
"no image"
19+
Exit 1
20+
}
21+
22+
$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
23+
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
24+
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
25+
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
26+
$encoder.Save($stream) | out-null
27+
$stream.Dispose() | out-null
28+
29+
$imagePath
30+
}
31+
32+
try {
33+
# For WIN10
34+
$file = Get-Clipboard -Format FileDropList
35+
if ($file -ne $null) {
36+
Convert-Path $file
37+
Exit 1
38+
}
39+
} catch {
40+
# For WIN7 WIN8 WIN10
41+
main
42+
}
43+
44+
main

src/utils/getClipboardImage.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ import PicGo from '../core/PicGo'
22
import path from 'path'
33
import { spawn } from 'child_process'
44
import dayjs from 'dayjs'
5+
import os from 'os'
6+
7+
const getCurrentPlatform = (): string => {
8+
let platform = process.platform
9+
if (platform !== 'win32') {
10+
return platform
11+
} else {
12+
let currentOS = os.release().split('.')[0]
13+
if (currentOS === '10') {
14+
return 'win10'
15+
} else {
16+
return 'win32'
17+
}
18+
}
19+
}
520

621
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
722
const getClipboardImage = (ctx: PicGo): Promise<any> => {
823
const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
924
return new Promise((resolve: any, reject: any): any => {
10-
let platform: string = process.platform
25+
let platform: string = getCurrentPlatform()
1126
let execution = null
1227
// for PicGo GUI
1328
let env = ctx.config.PICGO_ENV === 'GUI'
@@ -16,13 +31,13 @@ const getClipboardImage = (ctx: PicGo): Promise<any> => {
1631
} = {
1732
'darwin': env ? path.join(ctx.baseDir,'./mac.applescript') : './clipboard/mac.applescript',
1833
'win32': env ? path.join(ctx.baseDir, 'windows.ps1') : './clipboard/windows.ps1',
34+
'win10': env ? path.join(ctx.baseDir, 'windows10.ps1') : './clipboard/windows10.ps1',
1935
'linux': env ? path.join(ctx.baseDir, 'linux.sh') : './clipboard/linux.sh'
2036
}
2137
const scriptPath = env ? platformPaths[platform] : path.join(__dirname, platformPaths[platform])
2238
if (platform === 'darwin') {
2339
execution = spawn('osascript', [scriptPath, imagePath])
24-
25-
} else if (platform === 'win32') {
40+
} else if (platform === 'win32' || platform === 'win10') {
2641
execution = spawn('powershell', [
2742
'-noprofile',
2843
'-noninteractive',

0 commit comments

Comments
 (0)