Save downloaded files to a database?

I use the drag and drop function to stop uploading to upload multiple images. Therefore, I have a requirement to store some information about the uploaded images in the database:

  • Create two images of different sizes (large, large) and original. - (works well creates two blob files)
  • Manage individual records in a database to store information for three different sizes of individual images after upload. For example: suppose I found three different images - image_thumbs.jpg, image_large.jpg, image_original.jpg , which will be stored in a database such as

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

So, I put the logic at the end of the server to get the qqparentuuid image , if it is a child image / blob, and if it is a parent, then get qquuid based on the UUID (qqparentuuid / qquuid), which I get from the downloaded images, check the database if there is a UUID (qqparentuuid / qquuid), then update the record with the current image name, otherwise insert a new record in the database with a respected UUID (qqparentuuid / qquuid).

This logic works correctly, but in a few cases this logic failed because the downloaded files are uploaded to the server in parallel and sometimes create three or two different lines in the database. on my side, this happens when files fall in parallel on the server not sequentially, and all the logic falls on the flop.

, , . :

<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:

$('#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());
})

, , .

+4

Source: https://habr.com/ru/post/1568278/


All Articles