Skip to content

Commit e22fb05

Browse files
author
tiann
committed
1. 使用临时文件替换/tmp目录下,在使用之后会自动删除\n 2. 支持图片压缩,QQ截图高清格式17M直接压缩为500K\n
1 parent c6a9ff2 commit e22fb05

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

clipboard.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
# -*- coding: utf-8 -*-
2-
import time, os
2+
import os
3+
import tempfile
4+
35
from AppKit import NSPasteboard, NSPasteboardTypePNG, NSPasteboardTypeTIFF, NSPasteboardTypeString
46

57
def get_paste_img_file():
8+
''' get a img file from clipboard;
9+
the return object is a `tempfile.NamedTemporaryFile`
10+
you can use the name field to access the file path.
11+
the tmp file will be delete as soon as possible(when gc happened or close explicitly)
12+
you can not just return a path, must hold the reference'''
13+
614
pb = NSPasteboard.generalPasteboard()
715
data_type = pb.types()
816
# if img file
9-
print data_type
10-
now = int(time.time() * 1000) # used for filename
11-
if NSPasteboardTypePNG in data_type:
12-
# png
17+
# print data_type
18+
# always generate png format img
19+
png_file = tempfile.NamedTemporaryFile(suffix="png")
20+
21+
supported_image_format = (NSPasteboardTypePNG, NSPasteboardTypeTIFF)
22+
if any(filter(lambda f: f in data_type, supported_image_format)):
23+
# do not care which format it is, we convert it to png finally
24+
# system screen shotcut is png, QQ is tiff
25+
tmp_img_file = tempfile.NamedTemporaryFile()
1326
data = pb.dataForType_(NSPasteboardTypePNG)
14-
filename = '%s.png' % now
15-
filepath = '/tmp/%s' % filename
16-
ret = data.writeToFile_atomically_(filepath, False)
17-
if ret:
18-
return filepath
19-
elif NSPasteboardTypeTIFF in data_type:
20-
# tiff
21-
data = pb.dataForType_(NSPasteboardTypeTIFF)
22-
filename = '%s.tiff' % now
23-
filepath = '/tmp/%s' % filename
24-
ret = data.writeToFile_atomically_(filepath, False)
25-
if ret:
26-
os.system('sips -s format tiff /tmp/%s.tiff --out /tmp/%s.png' % (now, now))
27-
return '/tmp/%s.png' % now
27+
ret = data.writeToFile_atomically_(tmp_img_file.name, False)
28+
if not ret: return
29+
30+
# convert it to png file
31+
os.system('sips -s format png %s --out %s' % (tmp_img_file.name, png_file.name))
32+
33+
# close the file explicitly
34+
tmp_img_file.close()
35+
return png_file
36+
2837
elif NSPasteboardTypeString in data_type:
2938
# string todo, recognise url of png & jpg
3039
pass
3140

41+
if __name__ == '__main__':
42+
get_paste_img_file()
3243

3344

3445

info.plist

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import util
8585
import os
8686
import subprocess
8787
import sys
88+
import time
8889
8990
if not os.path.exists(util.CONFIG_FILE):
9091
util.generate_config_file()
@@ -99,15 +100,17 @@ url = '%s/%s' % (config['url'], config['prefix'])
99100
100101
img_file = get_paste_img_file()
101102
if img_file:
102-
# has file
103-
name = os.path.split(img_file)[1]
104-
size_str = subprocess.check_output('sips -g pixelWidth %s | tail -n1 | cut -d" " -f4' % img_file, shell=True)
103+
# has image
104+
105+
# use time to generate a unique upload_file name, we can not use the tmp file name
106+
upload_name = "%s.png" % int(time.time() * 1000)
107+
size_str = subprocess.check_output('sips -g pixelWidth %s | tail -n1 | cut -d" " -f4' % img_file.name, shell=True)
105108
size = int(size_str.strip()) / 2
106-
markdown_url = '<img src="%s/%s" width="%d"/>' % (url, name, size)
109+
markdown_url = '<img src="%s/%s" width="%d"/>' % (url, upload_name, size)
107110
# make it to clipboard
108111
os.system("echo '%s' | pbcopy" % markdown_url)
109112
os.system('osascript -e \'tell application "System Events" to keystroke "v" using command down\'')
110-
ret = upload_qiniu(img_file)
113+
ret = upload_qiniu(img_file.name, upload_name)
111114
if not ret: util.notice("上传图片到图床失败,请检查网络后重试")
112115
else:
113116
util.notice("剪切版里没有图片!")

markdown img.alfredworkflow

537 Bytes
Binary file not shown.

upload.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
config = read_config()
88

9-
def upload_qiniu(path):
9+
def upload_qiniu(path, upload_name):
1010
''' upload file to qiniu'''
11-
q = Auth(config['ak'], config['sk'])
12-
dirname, filename = os.path.split(path)
13-
key = '%s/%s' % (config['prefix'], filename) # upload to qiniu's markdown dir
11+
q = Auth(config['ak'], config['sk'])
12+
key = '%s/%s' % (config['prefix'], upload_name) # upload to qiniu's markdown dir
1413

1514
token = q.upload_token(config['bucket'], key)
1615
ret, info = put_file(token, key, path, check_crc=True)

0 commit comments

Comments
 (0)