<commit_before><cfinclude template="/includes/_header.cfm">
<cfset numLabels=10>
<cfset numRelns=5>
<cfif not isdefined("debug")><cfset debug=false></cfif>
<!------------------------------------------------------->
<cfif action is "nothing">
	<p>
		These actually create Media
	</p>
	<a href="BulkloadMedia.cfm?action=validate&debug=true">validate&debug</a>
	<br><a href="BulkloadMedia.cfm?action=report">report</a>
	<br><a href="BulkloadMedia.cfm?action=cleanup">cleanup</a>
	<br><a href="BulkloadMedia.cfm?action=load">load</a>


	<p>
		These are handlers for uploaded image-containing ZIP files. Putting them here in an attempt to keep more organized.
		<br><a href="BulkloadMedia.cfm?action=unzip">unzip</a>
		<br><a href="BulkloadMedia.cfm?action=zip_rename">zip_rename</a>
		<br><a href="BulkloadMedia.cfm?action=zip_rename_confirm">zip_rename_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_makepreview">zip_makepreview</a>
		<br><a href="BulkloadMedia.cfm?action=zip_makepreview_confirm">zip_makepreview_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_s3ify">zip_s3ify</a>
		<br><a href="BulkloadMedia.cfm?action=zip_s3ify_confirm">zip_s3ify_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_notify_done">zip_notify_done</a>

	</p>
</cfif>

<!------------------------------------------------------------------------------------------------>
<cfif action is "zip_notify_done">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where (
			status like '%FATAL ERROR%' or
			status='loaded_to_s3') and
			status not like '%complete_email_sent%' and
			rownum=1
		</cfquery>
		<cfif d.recordcount is 0>
			<cfabort>
		</cfif>

		<cfif d.status contains "FATAL ERROR">
			<cfmail to="#d.email#" bcc="<EMAIL>" subject="ZIP upload status" cc="#Application.LogEmail#" from="<EMAIL>#" type="html">
				Dear #d.username#,
				<p>
					Your image zip upload job #d.jobname# has failed with error #d.status#
				</p>
				<p>
					Please review the instructions on the upload page, and contact us if you need assistance to resolve the problem.
				</p>
				<p>
					More information may be available at #application.serverRootUrl#/tools/uploadMedia.cfm?action=preview###d.zid#
				</p>
			</cfmail>

			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='complete_email_sent - ' || status where zid=#d.zid#
			</cfquery>
		<cfelse>
			<cfset utilities = CreateObject("component","component.utilities")>
			<cfset utilities.makeMBLDownloadFile(#d.zid#)>
			<cfmail to="#d.email#" bcc="<EMAIL>" subject="ZIP upload status" cc="#Application.LogEmail#" from="<EMAIL>#" type="html">
				Dear #d.username#,
				<p>
					Your image zip upload job #d.jobname# is complete.
				</p>
				<p>
					A file is available at #application.serverRootUrl#/download/media_bulk_zip#d.zid#.csv. This file will be deleted in three days,
					but may be regenerated from the more information link below.
				</p>
				<p>
					The file is NOT ready to upload in the media bulkloader.
				</p>
				<p>
					* TEMP_original_filename is the filename as supplied.
					<br>* TEMP_new_filename is the filename as loaded.
				</p>
				<p>
					Please delete these columns before attempting upload. Instructions for adding columns or data are available from the Media Bulkloader.
				</p>
				<p>
					Please do NOT delete the "MD5 checksum" (in MEDIA_LABEL_1 and MEDIA_LABEL_VALUE_1). This is important to preventing duplicate creation
					and ensuring that files have not inadvertantly been changed over time.
				</p>
				<p>
					More information may be available at #application.serverRootUrl#/tools/uploadMedia.cfm?action=preview###d.zid#
				</p>
			</cfmail>
			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='complete_email_sent' where zid=#d.zid#
			</cfquery>
		</cfif>
	</cfoutput>
</cfif>
<!------------------------------------------------------------------------------------------------>

<cfif action is "zip_s3ify_confirm">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where status='previewed' and rownum=1
		</cfquery>
		<cfif d.recordcount lt 1>
			<cfabort>
		</cfif>
		<cfquery name="d_f" datasource="uam_god">
			select distinct status from cf_temp_zipfiles where zid=#d.zid#
		</cfquery>
		<cfif valuelist(d_f.status) is "loaded_to_s3">
			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='loaded_to_s3' where zid=#d.zid#
			</cfquery>
		</cfif>
	</cfoutput>
</cfif>

<!------------------------------------------------------------------------------------------------>
<cfif action is "zip_s3ify">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where status='previewed' and rownum=1
		</cfquery>
		<cfif d.recordcount is 0>
			<cfabort>
		</cfif>
		<cfquery name="s3" datasource="uam_god" cachedWithin="#CreateTimeSpan(0,1,0,0)#">
			select S3_ENDPOINT,S3_ACCESSKEY,S3_SECRETKEY from cf_global_settings
		</cfquery>
		<!---- make a username bucket. This will create or return an error of some sort. ---->
		<cfset currentTime = getHttpTimeString( now() ) />
		<cfset contentType = "text/html" />
		<cfset bucket="#lcase(d.username)#">
		<cfset stringToSignParts = [
			    "PUT",
			    "",
			    contentType,
			    currentTime,
			    "/" & bucket
			] />
		<cfset stringToSign = arrayToList( stringToSignParts, chr( 10 ) ) />
		<cfset signature = binaryEncode(
			binaryDecode(
				hmac( stringToSign, s3.s3_secretKey, "HmacSHA1", "utf-8" ),
				"hex"
			),
			"base64"
		)>
		<cfhttp result="mkunamebkt"  method="put" url="#s3.s3_endpoint#/#bucket#">
			<cfhttpparam type="header" name="Authorization" value="AWS #s3.s3_accesskey#:#signature#"/>
		    <cfhttpparam type="header" name="Content-Type" value="#contentType#" />
		    <cfhttpparam type="header" name="Date" value="#currentTime#" />
		</cfhttp>


		<cfquery name="f" datasource="uam_god">
			select * from cf_temp_zipfiles where zid=#d.zid# and status='previewed' and rownum=1
		</cfquery>
		<cfloop query="f">
			<cffile variable="content" action="readBinary" file="#Application.webDirectory#/temp/#d.zid#/#new_filename#">
			<cfset lmd5 = createObject("component","includes.cfc.hashBinary").hashBinary(content)>
			<cfquery name="ckck" datasource="uam_god">
				select media_id from media_labels where MEDIA_LABEL='MD5 checksum' and LABEL_VALUE='#md5#'
			</cfquery>
			<cfif ckck.recordcount gt 0>
				<cfquery name="fail" datasource="uam_god">
					update cf_temp_zipload set status='FATAL ERROR: #new_filename# exists as #Application.serverRootURL#/media/#ckck.media_id#' where zid=#d.zid#
				</cfquery>
				<cfbreak>
			</cfif>
			<cfset fext=listlast(new_filename,".")>
			<cfif fext is "jpg" or fext is "jpeg">
				<cfset mimetype="image/jpeg">
				<cfset mediatype="image">
			<cfelseif fext is "dng">
				<cfset mimetype="image/dng">
				<cfset mediatype="image">
			<cfelseif fext is "pdf">
				<cfset mimetype="application/pdf">
				<cfset mediatype="text">
			<cfelseif fext is "png">
				<cfset mimetype="image/png">
				<cfset mediatype="image">
			<cfelseif fext is "txt">
				<cfset mimetype="text/plain">
				<cfset mediatype="text">
			<cfelseif fext is "wav"><commit_msg>.<commit_after><cfinclude template="/includes/_header.cfm">
<cfset numLabels=10>
<cfset numRelns=5>
<cfif not isdefined("debug")><cfset debug=false></cfif>
<!------------------------------------------------------->
<cfif action is "nothing">
	<p>
		These actually create Media
	</p>
	<a href="BulkloadMedia.cfm?action=validate&debug=true">validate&debug</a>
	<br><a href="BulkloadMedia.cfm?action=report">report</a>
	<br><a href="BulkloadMedia.cfm?action=cleanup">cleanup</a>
	<br><a href="BulkloadMedia.cfm?action=load">load</a>


	<p>
		These are handlers for uploaded image-containing ZIP files. Putting them here in an attempt to keep more organized.
		<br><a href="BulkloadMedia.cfm?action=unzip">unzip</a>
		<br><a href="BulkloadMedia.cfm?action=zip_rename">zip_rename</a>
		<br><a href="BulkloadMedia.cfm?action=zip_rename_confirm">zip_rename_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_makepreview">zip_makepreview</a>
		<br><a href="BulkloadMedia.cfm?action=zip_makepreview_confirm">zip_makepreview_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_s3ify">zip_s3ify</a>
		<br><a href="BulkloadMedia.cfm?action=zip_s3ify_confirm">zip_s3ify_confirm</a>
		<br><a href="BulkloadMedia.cfm?action=zip_notify_done">zip_notify_done</a>

	</p>
</cfif>

<!------------------------------------------------------------------------------------------------>
<cfif action is "zip_notify_done">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where (
			status like '%FATAL ERROR%' or
			status='loaded_to_s3') and
			status not like '%complete_email_sent%' and
			rownum=1
		</cfquery>
		<cfif d.recordcount is 0>
			<cfabort>
		</cfif>

		<cfif d.status contains "FATAL ERROR">
			<cfmail to="#d.email#" bcc="<EMAIL>" subject="ZIP upload status" cc="#Application.LogEmail#" from="<EMAIL>@#<EMAIL>#" type="html">
				Dear #d.username#,
				<p>
					Your image zip upload job #d.jobname# has failed with error #d.status#
				</p>
				<p>
					Please review the instructions on the upload page, and contact us if you need assistance to resolve the problem.
				</p>
				<p>
					More information may be available at #application.serverRootUrl#/tools/uploadMedia.cfm?action=preview###d.zid#
				</p>
			</cfmail>

			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='complete_email_sent - ' || status where zid=#d.zid#
			</cfquery>
		<cfelse>
			<cfset utilities = CreateObject("component","component.utilities")>
			<cfset utilities.makeMBLDownloadFile(#d.zid#)>
			<cfmail to="#d.email#" bcc="<EMAIL>" subject="ZIP upload status" cc="#Application.LogEmail#" from="zip<EMAIL>@#Application.<EMAIL>Email#" type="html">
				Dear #d.username#,
				<p>
					Your image zip upload job #d.jobname# is complete.
				</p>
				<p>
					A file is available at #application.serverRootUrl#/download/media_bulk_zip#d.zid#.csv. This file will be deleted in three days,
					but may be regenerated from the more information link below.
				</p>
				<p>
					The file is NOT ready to upload in the media bulkloader.
				</p>
				<p>
					* TEMP_original_filename is the filename as supplied.
					<br>* TEMP_new_filename is the filename as loaded.
				</p>
				<p>
					Please delete these columns before attempting upload. Instructions for adding columns or data are available from the Media Bulkloader.
				</p>
				<p>
					Please do NOT delete the "MD5 checksum" (in MEDIA_LABEL_1 and MEDIA_LABEL_VALUE_1). This is important to preventing duplicate creation
					and ensuring that files have not inadvertantly been changed over time.
				</p>
				<p>
					More information may be available at #application.serverRootUrl#/tools/uploadMedia.cfm?action=preview###d.zid#
				</p>
			</cfmail>
			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='complete_email_sent' where zid=#d.zid#
			</cfquery>
		</cfif>
	</cfoutput>
</cfif>
<!------------------------------------------------------------------------------------------------>

<cfif action is "zip_s3ify_confirm">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where status='previewed' and rownum=1
		</cfquery>
		<cfif d.recordcount lt 1>
			<cfabort>
		</cfif>
		<cfquery name="d_f" datasource="uam_god">
			select distinct status from cf_temp_zipfiles where zid=#d.zid#
		</cfquery>
		<cfif valuelist(d_f.status) is "loaded_to_s3">
			<cfquery name="r" datasource="uam_god">
				update cf_temp_zipload set status='loaded_to_s3' where zid=#d.zid#
			</cfquery>
		</cfif>
	</cfoutput>
</cfif>

<!------------------------------------------------------------------------------------------------>
<cfif action is "zip_s3ify">
	<cfoutput>
		<cfquery name="d" datasource="uam_god">
			select * from cf_temp_zipload where status='previewed' and rownum=1
		</cfquery>
		<cfdump var=#d#>
		<cfif d.recordcount is 0>
			<cfabort>
		</cfif>
		<cfquery name="s3" datasource="uam_god" cachedWithin="#CreateTimeSpan(0,1,0,0)#">
			select S3_ENDPOINT,S3_ACCESSKEY,S3_SECRETKEY from cf_global_settings
		</cfquery>
		<!---- make a username bucket. This will create or return an error of some sort. ---->
		<cfset currentTime = getHttpTimeString( now() ) />
		<cfset contentType = "text/html" />
		<cfset bucket="#lcase(d.username)#">
		<cfset stringToSignParts = [
			    "PUT",
			    "",
			    contentType,
			    currentTime,
			    "/" & bucket
			] />
		<cfset stringToSign = arrayToList( stringToSignParts, chr( 10 ) ) />
		<cfset signature = binaryEncode(
			binaryDecode(
				hmac( stringToSign, s3.s3_secretKey, "HmacSHA1", "utf-8" ),
				"hex"
			),
			"base64"
		)>
		<cfhttp result="mkunamebkt"  method="put" url="#s3.s3_endpoint#/#bucket#">
			<cfhttpparam type="header" name="Authorization" value="AWS #s3.s3_accesskey#:#signature#"/>
		    <cfhttpparam type="header" name="Content-Type" value="#contentType#" />
		    <cfhttpparam type="header" name="Date" value="#currentTime#" />
		</cfhttp>


		<cfquery name="f" datasource="uam_god">
			select * from cf_temp_zipfiles where zid=#d.zid# and status='previewed' and rownum=1
		</cfquery>
		<cfdump var=#f#>
		<cfloop query="f">
			<cffile variable="content" action="readBinary" file="#Application.webDirectory#/temp/#d.zid#/#new_filename#">
			<cfset lmd5 = createObject("component","includes.cfc.hashBinary").hashBinary(content)>
			<cfquery name="ckck" datasource="uam_god">
				select media_id from media_labels where MEDIA_LABEL='MD5 checksum' and LABEL_VALUE='#md5#'
			</cfquery>
			<cfif ckck.recordcount gt 0>
				<cfquery name="fail" datasource="uam_god">
					update cf_temp_zipload set status='FATAL ERROR: #new_filename# exists as #Application.serverRootURL#/media/#ckck.media_id#' where zid=#d.zid#
				</cfquery>
				<cfbreak>
			</cfif>
			<cfset fext=listlast(new_filename,".")>
			<cfif fext is "jpg" or fext is "jpeg">
				<cfset mimetype="image/jpeg">
				<cfset mediatype="image">
			<cfelseif fext is "dng">
				<cfset mimetype="image/dng">
				<cfset mediatype="image">
			<cfelseif fext is "pdf">
				<cfset mimetype="application/pdf">
				<cfset mediatype="text">
			<cfelseif fext is "png">
				<cfset mimetype="image/png">
				<cfset mediatype="image">
			<cfelseif fext is "txt">
				<cfset mimetype="text/plain">
				<cfset mediatype="text">
			<cfelseif fext is "wav">
{'fraction_non_alphanumeric': 0.1878679994522799, 'fraction_numerical': 0.006298781322744078, 'mean_word_length': 3.4357728515031885, 'pattern_counts': {'":': 0, '<': 333, '<?xml version=': 0, '>': 333, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4603578', 'n_tokens_mistral': 5836, 'n_tokens_neox': 5097, 'n_words': 1153}