|
17 | 17 | def get_random_bit(length): |
18 | 18 | return ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(length)) |
19 | 19 |
|
20 | | -def capture_text(): |
| 20 | +def capture_text(extension): |
21 | 21 | # global name 'subprocess' is not defined |
22 | 22 | text_file = subprocess.check_output("pbpaste", shell=True) |
23 | 23 | random_bit = get_random_bit(10) |
24 | | - file_name = 'clip-'+random_bit+'.txt' |
| 24 | + file_name = 'clip-'+random_bit+'.'+extension |
25 | 25 | file_path = os.path.join(tempfile.mkdtemp(), file_name) |
26 | 26 | with open(file_path, "wb") as f: |
27 | 27 | f.write(text_file) |
28 | | - return file_path, file_name, 'text/plain' |
| 28 | + # return content type based on extension. |
| 29 | + content_type = mimetypes.MimeTypes().guess_type(file_path)[0] |
| 30 | + return file_path, file_name, content_type |
29 | 31 |
|
30 | 32 | def capture(): |
31 | 33 | random_bit = get_random_bit(5) |
@@ -54,9 +56,15 @@ def capture(): |
54 | 56 |
|
55 | 57 | def main(wf): |
56 | 58 | import boto3 |
| 59 | + # print all arguments for debug. |
| 60 | + #for arg in sys.argv: |
| 61 | + # print (arg) |
| 62 | + #exit() |
57 | 63 | # if cmd is pressed we assume it is text capture. |
58 | 64 | if sys.argv[1] == "text": |
59 | | - file_path, file_name, content_type = capture_text() |
| 65 | + # extension in 2nd argument |
| 66 | + extension = sys.argv[2] if len(sys.argv) > 2 else 'txt' |
| 67 | + file_path, file_name, content_type = capture_text(extension) |
60 | 68 | else: |
61 | 69 | file_path, file_name, content_type = capture() |
62 | 70 | account_id = os.getenv('cf_account_id') |
|
0 commit comments