-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload
executable file
·58 lines (46 loc) · 1.73 KB
/
upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python3
from helpers.config import get_api_settings, get_logger
from helpers.mutations import stagedUploadsCreate, multipart_form, fileCreate
from helpers.queries import fileUrlQuery
import argparse
import time
def main():
logger = get_logger()
parser = argparse.ArgumentParser(description='Uploads a file.')
parser.add_argument('filename', type=str, help='The json file to upload')
args = parser.parse_args()
url, link, headers = get_api_settings()
staging_variables = {
"filename": args.filename,
"httpMethod": "POST",
"mimeType": "text/json",
"resource": "FILE",
}
res = stagedUploadsCreate(url, headers, staging_variables)
logger.info(res)
data = res["data"]["stagedUploadsCreate"]["stagedTargets"][0]
targets = {param["name"]: param["value"] for param in data["parameters"]}
targets["url"] = data["url"]
targets["resourceUrl"] = data["resourceUrl"]
r = multipart_form(targets, args.filename, f"./{args.filename}")
if r.status_code == 201:
logger.info("Form submitted successfully")
else:
logger.warning("Form submission failed")
creation_variables = {
"alt": args.filename,
"contentType": staging_variables["resource"],
"originalSource": targets["resourceUrl"],
}
final = fileCreate(url, headers, creation_variables)
logger.info(final)
file_id = final['data']['fileCreate']['files'][0]['id']
logger.info(f"Visit {link}")
# delay until the CDN url is available
time.sleep(1)
file_url = fileUrlQuery(url, headers, file_id)
logger.info(f"File ID: {file_id}")
if file_url:
logger.info(f"See it on the CDN at {file_url}")
if __name__ == '__main__':
main()