-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileMgr_S3.cfc
222 lines (171 loc) · 8.56 KB
/
FileMgr_S3.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<cfcomponent displayname="File Manager" extends="FileMgr" output="no">
<cffunction name="init" access="public" returntype="FileMgr" output="no" hint="I instantiate and return this object.">
<cfargument name="UploadPath" type="string" default="" hint="The file path for uploads.">
<cfargument name="UploadURL" type="string" default="http://s3.amazonaws.com/" hint="The URL path for uploads.">
<cfargument name="Bucket" type="string" required="true" hint="AWS S3 Bucket name.">
<cfargument name="Credentials" type="any" required="true" hint="AWS Credentials.">
<cfset setUpVariables(ArgumentCollection=Arguments)>
<cfset Variables.StorageMechanism = "S3">
<!--- Make sure needed S3 credentials exist. --->
<cfif NOT ( Variables.Credentials.has("AccessKey") AND Variables.Credentials.has("SecretKey") AND Variables.Credentials.has("CanonicalUserID") )>
<cfthrow message="S3 FileMgr requires AWS credentials (AccessKey,SecretKey,CanonicalUserID)." type="FileMgr">
</cfif>
<cfif NOT Len(Trim(Variables.UploadPath))>
<cfset Variables.UploadPath = 's3://' & Variables.Credentials.get("AccessKey") & ':' & Variables.Credentials.get("SecretKey") & '@' & Variables.Bucket & '/'>
</cfif>
<cfif Variables.UploadURL CONTAINS "s3.amazonaws.com" AND NOT Variables.UploadURL CONTAINS Arguments.Bucket>
<cfset Variables.UploadURL = UploadURL & Arguments.Bucket & "/">
</cfif>
<cfreturn This>
</cffunction>
<cffunction name="convertFolder" access="public" returntype="string" output="no">
<cfargument name="Folder" type="string" required="yes">
<cfargument name="delimiter" type="string" default="/">
<cfreturn LCase(Super.convertFolder(ArgumentCollection=Arguments))>
</cffunction>
<cffunction name="getDirDelim" acess="public" returntype="string" output="no">
<cfif NOT StructKeyExists(variables,"dirdelim")>
<cfset variables.dirdelim = "/">
</cfif>
<cfreturn variables.dirdelim>
</cffunction>
<cffunction name="makedir" access="public" returntype="any" output="no" hint="I make a directory (if it doesn't exist already).">
<cfargument name="Directory" type="string" required="yes">
<cfset Super.makedir(ArgumentCollection=Arguments)>
</cffunction>
<cffunction name="makedir_private" access="private" returntype="any" output="no" hint="I make a directory.">
<cfargument name="Directory" type="string" required="yes">
<cfdirectory action="CREATE" directory="#Arguments.Directory#">
</cffunction>
<cffunction name="makeFolder" access="public" returntype="void" output="no">
<cfargument name="Folder" type="string" required="yes">
<!--- We don't actually need to make folders on S3. --->
<cfset var foo = "bar">
</cffunction>
<cffunction name="setFilePermissions" access="public" returntype="void" output="no">
<cfargument name="destination" type="string" required="yes">
<cfset StoreSetACL("#Arguments.destination#",getStandardS3Permissions())>
</cffunction>
<cffunction name="uploadFile" access="public" returntype="any" output="no">
<cfargument name="FieldName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfargument name="NameConflict" type="string" default="Error">
<cfargument name="TempDirectory" default="#variables.TempDir#">
<cfset var destination = getDirectory(argumentCollection=arguments)>
<cfset var CFFILE = StructNew()>
<cfset var sOrigFile = 0>
<cfset var tempPath = "">
<cfset var serverPath = "">
<cfset var skip = false>
<cfset var dirdelim = '/' />
<cfset var result = "">
<!--- Make sure the destination exists. --->
<cfif StructKeyExists(arguments,"Folder")>
<cfset makeFolder(arguments.Folder)>
</cfif>
<!--- Set default extensions --->
<cfif NOT StructKeyExists(arguments,"extensions")>
<cfset arguments.extensions = variables.DefaultExtensions>
</cfif>
<!--- Upload to temp directory. --->
<cfif StructKeyExists(Form,Arguments.FieldName)>
<cfif StructKeyExists(arguments,"accept")>
<cfif ListFindNoCase(arguments.accept,"application/msword") AND NOT ListFindNoCase(arguments.accept,"application/unknown")>
<cfset arguments.accept = ListAppend(arguments.accept,"application/unknown")>
</cfif>
<cfif ListFindNoCase(arguments.accept,"application/vnd.ms-excel") AND NOT ListFindNoCase(arguments.accept,"application/octet-stream")>
<cfset arguments.accept = ListAppend(arguments.accept,"application/octet-stream")>
</cfif>
<cffile action="UPLOAD" filefield="#Arguments.FieldName#" destination="#destination##cleanFileName(getClientFileName(Arguments.FieldName))#" nameconflict="#Arguments.NameConflict#" result="CFFILE" accept="#arguments.accept#">
<cfelse>
<cffile action="UPLOAD" filefield="#Arguments.FieldName#" destination="#destination##cleanFileName(getClientFileName(Arguments.FieldName))#" nameconflict="#Arguments.NameConflict#" result="CFFILE">
</cfif>
<cfset serverPath = ListAppend(CFFILE.ServerDirectory, CFFILE.ServerFile, getDirDelim())>
<cfelse>
<cffile destination="#Arguments.TempDirectory#" source="#Arguments.FieldName#" action="copy" result="CFFILE">
<cfset serverPath = ListAppend(CFFILE.ServerDirectory, CFFILE.ServerFile, getDirDelim())>
</cfif>
<!--- Check file extension --->
<cfif
Len(arguments.extensions)
AND NOT ListFindNoCase(arguments.extensions,ListLast(serverPath,"."))
>
<!--- Bad file extension. Delete file. --->
<cffile action="DELETE" file="#serverPath#">
<cfreturn StructNew()>
</cfif>
<cfset StoreSetMetadata("#serverPath#",convertS3MetaFromCFFILE(CFFILE))>
<!--- set permissions on the newly created file on S3 --->
<cfset setFilePermissions(serverPath)>
<cfset CFFILE.ServerDirectory = getDirectoryFromPath(serverPath)>
<cfset CFFILE.ServerFile = getFileFromPath(serverPath)>
<cfset CFFILE.ServerFileName = ReReplaceNoCase(CFFILE.ServerFile,"\.#CFFILE.SERVERFILEEXT#$","")>
<cfif StructKeyExists(arguments,"return") AND isSimpleValue(arguments.return)>
<cfif arguments.return EQ "name">
<cfset arguments.return = "ServerFile">
</cfif>
<cfif StructKeyExists(CFFILE,arguments.return)>
<cfset result = CFFILE[arguments.return]>
<cfif isSimpleValue(result) AND isSimpleValue(variables.dirdelim)>
<cfset result = ListLast(result,variables.dirdelim)>
</cfif>
</cfif>
<cfelse>
<cfset result = CFFILE>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="writeFile" access="public" returntype="string" output="no" hint="I save a file.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Contents" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var destination = Super.writeFile(ArgumentCollection=Arguments)>
<!--- set permissions on the newly created file on S3 --->
<cfset setFilePermissions(destination)>
<cfreturn destination>
</cffunction>
<cffunction name="writeBinaryFile" access="public" returntype="string" output="no" hint="I save a file.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Contents" type="binary" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var destination = Super.writeBinaryFile(ArgumentCollection=Arguments)>
<!--- set permissions on the newly created file on S3 --->
<cfset setFilePermissions(destination)>
<cfreturn destination>
</cffunction>
<cffunction name="getStandardS3Permissions" access="private" returntype="array" output="no">
<cfset var perms = [{group="all", permission="read"},{id="#Variables.Credentials.get('CanonicalUserID')#", permission="full_control"}]>
<cfreturn perms>
</cffunction>
<cffunction name="convertS3MetaFromCFFILE" access="private" returntype="struct" output="no">
<cfargument name="CFFILE" type="struct" required="yes">
<cfset var sResult = {
last_modified=GetHTTPTimeString(Arguments.CFFILE.TIMELASTMODIFIED),
date=GetHTTPTimeString(Arguments.CFFILE.TIMECREATED),
content_length=JavaCast("String",Arguments.CFFILE.FILESIZE),
content_type=getMimeType(Arguments.CFFILE.ServerFile)
}>
<!--- OTHER KEYS:
owner=
etag=
content_encoding=
content_disposition=
content_language=
content_md5=
md5_hash=
--->
<cfreturn sResult>
</cffunction>
<cffunction name="getClientFileName" returntype="string" output="false" hint="">
<cfargument name="fieldName" required="true" type="string" hint="Name of the Form field" />
<cfset var tmpPartsArray = Form.getPartsArray() />
<cfif IsDefined("tmpPartsArray")>
<cfloop array="#tmpPartsArray#" index="local.tmpPart">
<cfif local.tmpPart.isFile() AND local.tmpPart.getName() EQ arguments.fieldName> <!--- --->
<cfreturn local.tmpPart.getFileName() />
</cfif>
</cfloop>
</cfif>
<cfreturn "" />
</cffunction>
</cfcomponent>