我们有解析.apk文件的Python代码。我们希望用JavaScript来完成,然后直接将文件上传到S3。有可能吗?如果是,我们怎么做?如果需要,我们可以使用现有的插件。在
下面是解析文件的Python代码:import os
import urllib2
import StringIO
import re
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from androguard.core.bytecodes.apk import APK
from storages.backends.s3boto import S3BotoStorage
from parser import Parser
class APKParser(Parser):
def __init__(self, filepath):
if filepath.startswith("http://") or filepath.startswith("https://"):
conn = urllib2.urlopen(filepath)
self.apk = APK(conn.read(), raw=True)
conn.close()
else:
self.apk = APK(filepath)
self.apk_xml = self.apk.get_AndroidManifest()
def get_version(self):
manifest = self.apk_xml.getElementsByTagName("manifest")[0]
return manifest.getAttribute("android:versionName")
def get_name(self):
app = self.apk_xml.getElementsByTagName("application")[0]
name = app.getAttribute("android:label")
if name.startswith("@"):
package_parser = self.apk.get_android_resources()
name = ''
for package_name in package_parser.get_packages_names():
name = package_parser.get_string(package_name, 'app_name')
if name:
name = name[1]
break
return name
def get_package_name(self):
return self.apk.package
def get_image(self, content_item):
storage = S3BotoStorage(bucket=settings.AWS_MANAGED_CONTENT_SETS_STORAGE_BUCKET_NAME,
secure_urls=settings.SECURE_IMAGES_URL)
filepath = ""
for apk_file in self.apk.get_files():
if re.match("(res/drawable.*/(icon|logo).*)", apk_file):
filepath = apk_file
break
if filepath:
try:
fileext = os.path.splitext(filepath)[1][1:]
file_io = StringIO.StringIO()
file_io.write(self.apk.get_file(filepath))
icon_filename = "%s.%s" % (self.get_name(), fileext)
inmemory_file = InMemoryUploadedFile(file_io, None, icon_filename, 'image/%s' % fileext,
file_io.len, None)
inmemory_file.seek(0)
upl_file = uploaded_filename(content_item, inmemory_file.name)
storage._save(upl_file, inmemory_file)
file_io.close()
return dict(url=storage.url(upl_file), path=upl_file)
except:
pass
return dict()