Search in sources :

Example 1 with ApkMeta

use of net.dongliu.apk.parser.bean.ApkMeta in project webapp by elimu-ai.

the class AppCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(ApplicationVersion applicationVersion, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model, HttpSession session, @PathVariable Long appGroupId, @PathVariable Long projectId, @PathVariable Long appCategoryId) {
    logger.info("handleSubmit");
    Project project = projectDao.read(projectId);
    AppCategory appCategory = appCategoryDao.read(appCategoryId);
    AppGroup appGroup = appGroupDao.read(appGroupId);
    boolean isUpdateOfExistingApplication = applicationVersion.getApplication() != null;
    logger.info("isUpdateOfExistingApplication: " + isUpdateOfExistingApplication);
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    String packageName = null;
    if (multipartFile.isEmpty()) {
        result.rejectValue("bytes", "NotNull");
    } else {
        try {
            byte[] bytes = multipartFile.getBytes();
            Integer fileSizeInKb = bytes.length / 1024;
            logger.info("fileSizeInKb: " + fileSizeInKb + " (" + (fileSizeInKb / 1024) + "MB)");
            String contentType = multipartFile.getContentType();
            logger.info("contentType: " + contentType);
            // Auto-detect applicationId, versionCode, etc.
            ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(bytes);
            ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
            packageName = apkMeta.getPackageName();
            logger.info("packageName: " + packageName);
            String label = apkMeta.getLabel();
            logger.info("label: " + label);
            byte[] icon = byteArrayApkFile.getIconFile().getData();
            logger.info("icon.length: " + (icon.length / 1024) + "kB");
            Integer versionCode = apkMeta.getVersionCode().intValue();
            logger.info("versionCode: " + versionCode);
            String versionName = apkMeta.getVersionName();
            logger.info("versionName: " + versionName);
            Integer minSdkVersion = Integer.valueOf(apkMeta.getMinSdkVersion());
            logger.info("minSdkVersion: " + minSdkVersion);
            // Check if Application already exists in the same AppCategory
            // TODO
            applicationVersion.setBytes(bytes);
            applicationVersion.setFileSizeInKb(fileSizeInKb);
            applicationVersion.setChecksumMd5(ChecksumHelper.calculateMD5(bytes));
            applicationVersion.setContentType(contentType);
            applicationVersion.setVersionCode(versionCode);
            applicationVersion.setVersionName(versionName);
            applicationVersion.setLabel(label);
            applicationVersion.setMinSdkVersion(minSdkVersion);
            applicationVersion.setIcon(icon);
            applicationVersion.setTimeUploaded(Calendar.getInstance());
            applicationVersion.setContributor(contributor);
            if (isUpdateOfExistingApplication) {
                // Verify that the packageName of the APK update matches that of the Application
                if (!applicationVersion.getApplication().getPackageName().equals(packageName)) {
                    result.rejectValue("application", "NonUnique");
                }
            }
            if (!isUpdateOfExistingApplication) {
                // Verify that an Application with identical packageName has not already been uploaded withing the same Project
                Application existingApplication = applicationDao.readByPackageName(project, packageName);
                if (existingApplication != null) {
                    result.rejectValue("application", "NonUnique", new String[] { "application" }, null);
                }
            }
            if (isUpdateOfExistingApplication) {
                // Verify that the versionCode is higher than previous ones
                List<ApplicationVersion> existingApplicationVersions = applicationVersionDao.readAll(applicationVersion.getApplication());
                for (ApplicationVersion existingApplicationVersion : existingApplicationVersions) {
                    if (existingApplicationVersion.getVersionCode() >= applicationVersion.getVersionCode()) {
                        result.rejectValue("versionCode", "TooLow");
                        break;
                    }
                }
            }
        } catch (IOException ex) {
            logger.error(ex);
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("project", project);
        model.addAttribute("appCategory", appCategory);
        model.addAttribute("appGroup", appGroup);
        return "project/app/create";
    } else {
        Application application = applicationVersion.getApplication();
        logger.info("application: " + application);
        if (application == null) {
            // First-time upload for packageName
            // Create new Application
            application = new Application();
            // TODO: Add Locale to each Project?
            application.setLocale(contributor.getLocale());
            application.setPackageName(packageName);
            application.setApplicationStatus(ApplicationStatus.MISSING_APK);
            application.setContributor(contributor);
            application.setProject(project);
            applicationDao.create(application);
            appGroup.getApplications().add(application);
            appGroupDao.update(appGroup);
            applicationVersion.setApplication(application);
            applicationVersionDao.create(applicationVersion);
            application.setApplicationStatus(ApplicationStatus.ACTIVE);
            applicationDao.update(application);
        } else {
            // Update of existing packageName previously uploaded
            // Create new ApplicationVersion for the existing Application
            applicationVersionDao.create(applicationVersion);
        }
        // Refresh REST API cache
        // jsonService.refreshApplicationsInAppCollection(appCollection);
        jsonService.refreshApplicationsInAppCollection();
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String applicationDescription = !isUpdateOfExistingApplication ? "Application" : "APK version";
            String text = URLEncoder.encode(contributor.getFirstName() + " just uploaded a new " + applicationDescription + ":\n" + "• Project: \"" + project.getName() + "\"\n" + "• App Category: \"" + appCategory.getName() + "\"\n" + "• Package name: \"" + application.getPackageName() + "\"\n" + "• Version code: " + applicationVersion.getVersionCode() + "\n" + "See ") + "http://elimu.ai/project/" + project.getId() + "/app-category/" + appCategory.getId() + "/app-group/" + appGroup.getId() + "/app/" + application.getId() + "/edit";
            SlackApiHelper.postMessage("G6UR7UH2S", text, null, null);
        }
        if (!isUpdateOfExistingApplication) {
            return "redirect:/project/{projectId}/app-category/{appCategoryId}/app-group/{appGroupId}/app/list#" + application.getId();
        } else {
            return "redirect:/project/{projectId}/app-category/{appCategoryId}/app-group/{appGroupId}/app/" + application.getId() + "/edit";
        }
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Project(ai.elimu.model.project.Project) ByteArrayApkFile(net.dongliu.apk.parser.ByteArrayApkFile) AppGroup(ai.elimu.model.project.AppGroup) ApkMeta(net.dongliu.apk.parser.bean.ApkMeta) Application(ai.elimu.model.admin.Application) AppCategory(ai.elimu.model.project.AppCategory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ApkMeta

use of net.dongliu.apk.parser.bean.ApkMeta in project webapp by elimu-ai.

the class ApplicationVersionCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(ApplicationVersion applicationVersion, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model, HttpSession session) {
    logger.info("handleSubmit");
    logger.info("applicationVersion.getVersionCode(): " + applicationVersion.getVersionCode());
    if (applicationVersion.getVersionCode() == null) {
        result.rejectValue("versionCode", "NotNull");
    } else {
        // Verify that the versionCode is higher than previous ones
        List<ApplicationVersion> existingApplicationVersions = applicationVersionDao.readAll(applicationVersion.getApplication());
        for (ApplicationVersion existingApplicationVersion : existingApplicationVersions) {
            if (existingApplicationVersion.getVersionCode() >= applicationVersion.getVersionCode()) {
                result.rejectValue("versionCode", "TooLow");
                break;
            }
        }
    }
    if (multipartFile.isEmpty()) {
        result.rejectValue("bytes", "NotNull");
    } else {
        try {
            byte[] bytes = multipartFile.getBytes();
            if (applicationVersion.getBytes() != null) {
                String originalFileName = multipartFile.getOriginalFilename();
                logger.info("originalFileName: " + originalFileName);
                if (!originalFileName.endsWith(".apk")) {
                    result.rejectValue("bytes", "typeMismatch");
                }
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                applicationVersion.setContentType(contentType);
                applicationVersion.setBytes(bytes);
                Integer fileSizeInKb = bytes.length / 1024;
                logger.info("fileSizeInKb: " + fileSizeInKb + " (" + (fileSizeInKb / 1024) + "MB)");
                applicationVersion.setFileSizeInKb(fileSizeInKb);
                String checksumMd5 = ChecksumHelper.calculateMD5(bytes);
                logger.info("checksumMd5: " + checksumMd5);
                applicationVersion.setChecksumMd5(checksumMd5);
                ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(bytes);
                ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
                String versionName = apkMeta.getVersionName();
                logger.info("versionName: " + versionName);
                applicationVersion.setVersionName(versionName);
                String label = apkMeta.getLabel();
                logger.info("label: " + label);
                applicationVersion.setLabel(label);
                Integer minSdkVersion = Integer.valueOf(apkMeta.getMinSdkVersion());
                logger.info("minSdkVersion: " + minSdkVersion);
                applicationVersion.setMinSdkVersion(minSdkVersion);
                byte[] icon = byteArrayApkFile.getIconFile().getData();
                logger.info("icon.length: " + (icon.length / 1024) + "kB");
                applicationVersion.setIcon(icon);
            } else {
                result.rejectValue("bytes", "NotNull");
            }
        } catch (IOException ex) {
            logger.error(ex);
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("applicationVersion", applicationVersion);
        return "admin/application-version/create";
    } else {
        Contributor contributor = (Contributor) session.getAttribute("contributor");
        applicationVersion.setContributor(contributor);
        applicationVersion.setTimeUploaded(Calendar.getInstance());
        applicationVersionDao.create(applicationVersion);
        // Update the Application entity to reflect the latest changes
        Application application = applicationVersion.getApplication();
        if (application.getApplicationStatus() == ApplicationStatus.MISSING_APK) {
            // If first APK file, change status of application to "ACTIVE"
            application.setApplicationStatus(ApplicationStatus.ACTIVE);
        }
        applicationDao.update(application);
        // Refresh REST API cache
        jsonService.refreshApplications(application.getLocale());
        boolean isInfrastructureApp = "ai.elimu.appstore".equals(application.getPackageName()) || "ai.elimu.appstore_custom".equals(application.getPackageName()) || "ai.elimu.analytics".equals(application.getPackageName()) || "ai.elimu.launcher".equals(application.getPackageName()) || "ai.elimu.launcher_custom".equals(application.getPackageName());
        if (isInfrastructureApp) {
            jsonService.refreshApplicationsInAppCollection();
        }
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just uploaded a new APK version:\n" + "• Language: " + applicationVersion.getApplication().getLocale().getLanguage() + "\n" + "• Package name: \"" + applicationVersion.getApplication().getPackageName() + "\"\n" + "• Version: " + applicationVersion.getVersionCode() + "\n" + "• Start command: " + applicationVersion.getStartCommand());
            String iconUrl = contributor.getImageUrl();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.DEVELOPMENT), text, iconUrl, null);
        }
        return "redirect:/admin/application/edit/" + applicationVersion.getApplication().getId();
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) Contributor(ai.elimu.model.Contributor) ByteArrayApkFile(net.dongliu.apk.parser.ByteArrayApkFile) IOException(java.io.IOException) ApkMeta(net.dongliu.apk.parser.bean.ApkMeta) Application(ai.elimu.model.admin.Application) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.Contributor)2 Application (ai.elimu.model.admin.Application)2 ApplicationVersion (ai.elimu.model.admin.ApplicationVersion)2 IOException (java.io.IOException)2 ByteArrayApkFile (net.dongliu.apk.parser.ByteArrayApkFile)2 ApkMeta (net.dongliu.apk.parser.bean.ApkMeta)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 AppCategory (ai.elimu.model.project.AppCategory)1 AppGroup (ai.elimu.model.project.AppGroup)1 Project (ai.elimu.model.project.Project)1