Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to compare the size of the asset uploaded with the value from my custom OSGi configuration?

Avatar

Level 2

I have created an OSGi Config as follows:

ac6600_0-1652810995515.png

I am reading the values like:

ac6600_1-1652811036475.png

Now, what I want to do is I want to compare the size of the asset file uploaded with assetSize value [from OSGi] when activated = true. And if size of asset file > assetSize, then the asset won't get uploaded to DAM and will show a error message.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ac6600 

AssetDetails asset = new AssetDetails(assetResource);
asset.getOriginal.getsize();
if(asset.getSize() < assetSize) {
     // Do Something
}


Asset API: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/granite/ass...

Rendition API: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Re...

Hope that helps you!

Regards,

Santosh

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @ac6600 

AssetDetails asset = new AssetDetails(assetResource);
asset.getOriginal.getsize();
if(asset.getSize() < assetSize) {
     // Do Something
}


Asset API: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/granite/ass...

Rendition API: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Re...

Hope that helps you!

Regards,

Santosh

Avatar

Level 2

Hi @SantoshSai , thanks for the solve. One more thing(maybe a dumb question), where shall I write this piece of code. Like will this be part of servlet or shall I write this within the OSGi config or anything else?

Avatar

Community Advisor

Hi @ac6600 

I wound't do it in servlet, However, you can implement it in same class where you are reading configuration values by creating separate method or in separate service component.

Hope that helps!

Regards,
Santosh

Avatar

Level 2

Hi @SantoshSai if you don't mind, can you share the entire code that I need to write in servlet in order to achieve this functionality.

Thank you for all the help rendered.

Avatar

Community Advisor

Hi @ac6600 

/*
 *  Copyright 2015 Adobe Systems Incorporated
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.mysite.core.servlets;

import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.Asset;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

/**
 * Servlet that writes some sample content into the response. It is mounted for
 * all resources of a specific Sling resource type. The
 * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
 * idempotent. For write operations use the {@link SlingAllMethodsServlet}.
 */
@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes="mysite/components/page",
        methods=HttpConstants.METHOD_GET,
        extensions="txt")
@ServiceDescription("Simple Demo Servlet")
public class SimpleServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
        final Resource resource = req.getResource();

        Asset asset = resource.adaptTo(Asset.class);
        asset.getOriginal().getSize();

        resp.setContentType("text/plain");
        resp.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE));
    }
}


Regards,
Santosh

Avatar

Employee Advisor

The problem is that you need to hook into the asset upload process itself somehow. At best in a way, that terminates the request as soon as it crosses the configured value in the request body's size.

 

In AEM 6.x I don't think that there is a public API for it, and in AEM CS it is not possible at all, because the upload itself does not happen via the JVM at all.

The best way is either to restrict the frontend to prevent selecting larger assets to handle the cases when they are already uploaded.