4

I am using fine uploader drag and drop feature to upload multiple images. So i have requirement to maintain few information regarding uploaded images into database:

  1. Create two different size images (thumbs,larger ) and original. - (work well creates two blob files)
  2. Manage single records into database to save information for three different size of single images after uploading.for example : suppose i have found three different images - image_thumbs.jpg , image_large.jpg ,image_original.jpg that will stored on database like that

    1 | image_thumbs.jpg | image_large.jpg | image_original.jpg

So I have put logic on server end to get to image qqparentuuid if it child image/blob and if it parent then get qquuid,based on UUID(qqparentuuid/qquuid) what i get from uploaded images, check on database if UUID(qqparentuuid/qquuid) is exist then update record with current image name otherwise insert new record into database with respected UUID(qqparentuuid/qquuid).

This Logic work correctly but in few case this logic got failed, because uploaded files are uploaded on server parallel and some times create three or two different rows into a database. according to my side this happen when files are hit parallel on server not sequentially and all logic got flop.

I am not able to figure out, this is problem of fine uploader or it my end problem. I have share my server side logic below:

<cffunction name="UploadImages" access="public" output="true" returntype="any">
        <cfargument name="formData" type="struct" required="yes" />
        <cfset var result = true />
        <cfset var temp_Filename = ""/>
        <cfset var local = StructNew() />
        <cfset application.Config.imageDir = "#arguments.formData.imageDir#" />

        <!--- This function uploads each image called from FineUploader --->
        <!--- From here, you can call additional processing for resizing, renaming etc. --->
        <cftry>

        <!--- Here I rename uploaded files --->
            <cfset fileext = listlast(FORM.qqFileName,".")>
            <cfset filesize=#FORM.QQTOTALFILESIZE#>
            <cfset Rname= "IMG_"&dateFormat(now(), "mmddyyyy")&"_"&TimeFormat(now(), "HHmmssl")&"_"&FORM.QQTOTALFILESIZE>
            <cfset temp_Filename = "#Rname#.#fileext#">


            <!--- Here uploaded files on server directory--->
            <cffile action="upload"
            destination="#application.Config.imageDir#\#temp_Filename#"
            nameconflict="MAKEUNIQUE" 
            filefield="FORM.qqFile" />
            <cfset local.fileName = CFFILE.serverFile />

        <!--- check image qqparentuuid/qquuid on database for existance--->
        <cfquery name="getQuery" maxrows="1" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#">  
            select * from products where 
            <cfif isDefined("FORM.qqparentuuid")>
            parentuuid=<cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">
            <cfelse>
            parentuuid=<cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">
            </cfif>
        </cfquery> 



        <cfif val(getQuery.recordcount) gt 0>
            <!--- if Parentuuid  exist then update records respected UUID--->

            <cfquery name="updateQuery" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#"> 

            UPDATE products 
            SET
            <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
            thumbimage =  <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
            <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
            fullimage = <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
            <cfelse>
            masterimage = <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
            </cfif>
            modifiedBy=<cfqueryparam value = "Artwork admin" CFSQLType = "CF_SQL_VARCHAR">,
            modifiedTs =now()
            WHERE
            <cfif isDefined("FORM.qqparentuuid")>
            parentuuid = <cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">
            <cfelse>
            parentuuid = <cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">
            </cfif>

        </cfquery>

        <cfset productid=#getQuery.productid#>
        <cfelse>


            <!--- if Parentuuid not exist then insert new one with respect to UUID --->
            <cfquery name="insertQuery" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#" result="resultid"> 
            INSERT INTO products 
            ( 
                <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
                thumbimage,
                <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
                fullimage,
                <cfelse>
                masterimage,
                </cfif>
                parentuuid,
                title,
                createdby, 
                createdTs
                )
            VALUES
            (
                <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
                <cfif isDefined("FORM.qqparentuuid")>
                <cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">,
                <cfelse>
                <cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">,
                </cfif>
                <cfqueryparam value = "Untitled" CFSQLType = "CF_SQL_VARCHAR">,
                <cfqueryparam value = "Artwork admin" CFSQLType = "CF_SQL_VARCHAR">,
                now()
                );
        </cfquery>


        <cfset productid=#resultid.generated_key#>

    </cfif>

    <cfset result  = true />
    <cfcatch type="any">
    <cfset result = false />
    <cfrethrow />
    </cfcatch>
</cftry>

<cfset local.sResult = StructNew() />
<cfset local.sResult.result = result />
<cfif result>
    <cfset local.sResult.RETURNID = local.fileName />
    <cfset local.sResult.PRODUCTID = productid />
    <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
        <cfset local.sResult.IMAGESIZE = "small" />
        <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
            <cfset local.sResult.IMAGESIZE = "medium" />
            <cfelse>
                <cfset local.sResult.IMAGESIZE = "original" />
            </cfif>

            <cfelse>
                <cfset local.sResult.RETURNOUTPUT = "Place errors here later" />
            </cfif>

            <cfreturn local.sResult />

        </cffunction>

Fineuploader logic:

$('#fine-uploader').fineUploader({
        // debug: true,
        autoUpload: true,
        scaling: {
            sizes: [{
                name: "small",
                maxSize: 150
            }, {
            name: "medium",
            maxSize: 500
        }]

    },
    request: {
        endpoint: "Components/fineUploaderProxy.cfm?cfc=ImageDemo&functionName=UploadImages"
    },
    validation: {
        allowedExtensions: ['jpeg', 'jpg', 'png', 'gif'],
    },
    text: {
        uploadButton: 'Choose File'
    }
})
.on('progress', function(event, id, name, uploadedBytes, totalBytes) {
    //alert('Name-'+name+"uploadedBytes-" +uploadedBytes+"totalBytes-"+totalBytes);


})
.on('error', function(event, id, name, reason, responseJSON) {
    alert('There was an error with the upload.' + reason);
})

.on('complete', function(event, id, name, responseJSON) {


    if (responseJSON.success) {
        if (responseJSON.HASERRORS == true) {
            $('.qq-upload-success').hide();
            alert(responseJSON.DATA.RETURNOUTPUT);
        } else {
            $('.qq-upload-success').hide();

            if (responseJSON.IMAGESIZE == 'small') {
                ProductObjList.push(responseJSON.PRODUCTID);
                $('#outputImage').append('<li><a onclick="setArtDetailsURL(' + responseJSON.PRODUCTID + ');" class="thumbClass" id=' + responseJSON.PRODUCTID + '><img height="130" src="images/' + responseJSON.RETURNID + '" alt="" /></a></li>');



            }

        }
    }
})
.on('allComplete', function(event, succeeded, failed) {
    /* set final list of product after complete image upload*/
    $('#productlist').val(ProductObjList.toString());
})

So please suggest.

Thanks

Sameek Mishra
  • 9,174
  • 31
  • 92
  • 118
  • 3
    I suggest that you store individual records for each file instead of a single record with a delimited list. – Dan Bracuk Dec 19 '14 at 12:41
  • I suggest you `` the code that uploads with a scope of "session". Then, checking for that lock, that user will only upload in series instead of parallel – Paul Sturm May 08 '17 at 17:38

0 Answers0