-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileMgr.cfc
916 lines (717 loc) · 30.2 KB
/
FileMgr.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
<!--- 1.6 (Build 17) --->
<!--- Last Updated: 2011-12-14 --->
<!--- Information: sebtools.com --->
<cfcomponent displayname="File Manager">
<!--- %%Handle folders w/o write permission --->
<cffunction name="init" access="public" returntype="FileMgr" output="no" hint="I instantiate and return this object.">
<cfargument name="UploadPath" type="string" required="yes" hint="The file path for uploads.">
<cfargument name="UploadURL" type="string" default="" hint="The URL path for uploads.">
<cfargument name="Observer" type="any" required="no">
<cfset setUpVariables(ArgumentCollection=Arguments)>
<cfset Variables.StorageMechanism = "local">
<cfset makedir(Variables.UploadPath)>
<cfif StructKeyExists(Arguments,"Observer")>
<cfset Variables.Observer = Arguments.Observer>
</cfif>
<cftry>
<cfset Variables.MrECache = CreateObject("component","MrECache").init("FileMgr",CreateTimeSpan(2,0,0,0))>
<cfcatch>
</cfcatch>
</cftry>
<cfreturn This>
</cffunction>
<cffunction name="setUpVariables" access="private" returntype="void" output="no">
<cfset var key = "">
<cfloop collection="#Arguments#" item="key">
<cfset Variables[key] = Arguments[key]>
<cfif isObject(Arguments[key])>
<cfset This[key] = Arguments[key]>
</cfif>
</cfloop>
<cfset getDirDelim()>
<cfset variables.TempDir = GetTempDirectory()>
<cfset This["getDirectoryList"] = getMyDirectoryList>
<cfset variables["getDirectoryList"] = getMyDirectoryList>
<cfset This["DirectoryList"] = getMyDirectoryList>
<cfset variables["DirectoryList"] = getMyDirectoryList>
<cfset variables.DefaultExtensions = "ai,asx,avi,bmp,csv,dat,doc,docx,eps,fla,flv,gif,html,ico,jpeg,jpg,m4a,mov,mp3,mp4,mpa,mpg,mpp,pdf,png,pps,ppsx,ppt,pptx,ps,psd,qt,ra,ram,rar,rm,rtf,svg,swf,tif,txt,vcf,vsd,wav,wks,wma,wps,xls,xlsx,xml">
</cffunction>
<cffunction name="getDefaultExtensions" acess="public" returntype="string" output="no">
<cfreturn variables.DefaultExtensions>
</cffunction>
<cffunction name="getDirDelim" acess="public" returntype="string" output="no">
<cfset var result = "/">
<cfif NOT StructKeyExists(variables,"dirdelim")>
<cftry>
<cfset variables.dirdelim = CreateObject("java", "java.io.File").separator>
<cfcatch>
<cftry>
<cfif Server.OS.name CONTAINS "Windows">
<cfset variables.dirdelim = "\">
<cfelse>
<cfset variables.dirdelim = "/">
</cfif>
<cfcatch>
<cfif getCurrentTemplatePath() CONTAINS "/">
<cfset variables.dirdelim = "/">
<cfelse>
<cfset variables.dirdelim = "\">
</cfif>
</cfcatch>
</cftry>
</cfcatch>
</cftry>
</cfif>
<cfset result = variables.dirdelim>
<cfreturn result>
</cffunction>
<cffunction name="getMimeType" acess="public" returntype="string" output="no" hint="I return the mime-type for the given file (can be relative path, absolute path, or just the file name).">
<cfargument name="FileName" type="string" required="true">
<cfset var ext = ListLast(Arguments.FileName,".")>
<cfset loadMimeTypes()>
<cfif StructKeyExists(Variables.sTypes,ext)>
<cfreturn Variables.sTypes[ext]>
<cfelse>
<cfreturn "">
</cfif>
</cffunction>
<cffunction name="getStorageMechanism" acess="public" returntype="string" output="no" hint="I indicate how FileMgr is storing files.">
<cfreturn Variables.StorageMechanism>
</cffunction>
<cffunction name="LimitFileNameLength" acess="public" returntype="string" output="no">
<cfargument name="Length" type="numeric" required="true">
<cfargument name="Root" type="string" required="true">
<cfargument name="Suffix" type="string" default="">
<cfset var FileName = "">
<cfset var Dir = "">
<cfset var result = "">
<cfset var LeftLength = 0>
<cfif Arguments.Length EQ 0>
<cfset Arguments.Length = 1000>
</cfif>
<cfif arguments.Length GT 0>
<cfif Len(arguments.Suffix) EQ 0 AND ListLen(arguments.Root,".") GT 1>
<cfset arguments.Suffix = "." & ListLast(arguments.Root,".")>
<cfset arguments.Root = ListDeleteAt(arguments.Root,ListLen(arguments.Root,"."),".")>
</cfif>
<cfset result = "#arguments.Root##arguments.Suffix#">
<cfset FileName = getFileFromPath(result)>
<cfif ( result NEQ FileName )>
<cfset arguments.Root = getFileFromPath(arguments.Root)>
<cfset Dir = getDirectoryFromPath(result)>
</cfif>
<cfif Len(arguments.Suffix) GTE arguments.Length>
<cfthrow message="Suffix (#arguments.Suffix#) is too long for file length limit (#arguments.Length#)">
</cfif>
<cfif Len(FileName) GT arguments.Length>
<cfset LeftLength = arguments.Length - Len(arguments.Suffix) - 1>
<cfif LeftLength GT 0>
<cfset FileName = Left(arguments.Root,LeftLength) & "~" & arguments.Suffix>
<cfelse>
<cfset FileName = "~" & arguments.Suffix>
</cfif>
</cfif>
<cfset result = Dir & FileName>
<cfelse>
<cfset result = arguments.Root & arguments.Suffix>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="loadMimeTypes" access="public" returntype="any" output="false">
<cfset var CFHTTP = 0>
<cfset var sMimes = 0>
<cfset var key = "">
<cfif NOT StructKeyExists(Variables,"sTypes")>
<cfif StructKeyExists(Variables,"MrECache")>
<cfset Variables.sTypes = Variables.MrECache.meth(
Component=This,
MethodName="loadMimeTypes_RealTime"
)>
<cfelse>
<cfset loadMimeTypes_RealTime()>
</cfif>
</cfif>
<cfreturn Variables.sTypes>
</cffunction>
<cffunction name="loadMimeTypes_RealTime" access="public" returntype="any" output="false">
<cfset var CFHTTP = 0>
<cfset var sMimes = 0>
<cfset var key = "">
<cfset var ii = 0>
<cfset var filename = "mime-db.json">
<cfif NOT StructKeyExists(Variables,"sTypes")>
<cfset Variables.sTypes = {}>
<cfhttp
method="get"
result="CFHTTP"
url="https://cdn.rawgit.com/jshttp/mime-db/v1.30.0/db.json">
</cfhttp>
<cfscript>
if ( isJSON(CFHTTP.FileContent) ) {
sMimes = DeserializeJSON(CFHTTP.FileContent);
/* Save the JSON locally in case we can't reach it remotely later. */
if ( NOT FileExists(getFilePath(filename)) ) {
writeFile(filename,CFHTTP.FileContent);
}
} else if ( FileExists(getFilePath(filename)) ) {
/* If we fail to get the JSON locally, we'll use our saved copy (if we have one). */
sMimes = DeserializeJSON(readFile(filename));
}
//An error here means that the JSON retrieval failed both remotely and didn't exist locally.
for ( key in sMimes ) {
if ( StructKeyExists(sMimes[key],"extensions") AND ArrayLen(sMimes[key]["extensions"]) ) {
for ( ii=1; ii LTE ArrayLen(sMimes[key]["extensions"]); ii++ ) {
Variables.sTypes[sMimes[key]["extensions"][ii]] = key;
}
}
}
</cfscript>
</cfif>
<cfreturn Variables.sTypes>
</cffunction>
<cffunction name="makeFileCopy" access="public" returntype="string" output="no" hint="I make a copy of a file and return the new file name.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var path_old = getFilePath(argumentCollection=arguments)>
<cfset var path_new = createUniqueFileName(path_old)>
<cffile action="copy" source="#path_old#" destination="#path_new#">
<cfreturn ListLast(path_new,variables.dirdelim)>
</cffunction>
<cffunction name="copyFiles" access="public" returntype="void" output="no" hint="I copy files from one folder to another.">
<cfargument name="from" type="string" required="yes">
<cfargument name="to" type="string" required="yes">
<cfargument name="overwrite" type="boolean" default="false">
<cfset var dir_from = getDirectory(arguments.from)>
<cfset var dir_to = getDirectory(arguments.to)>
<cfset var qFiles = getDirectoryList(directory=dir_from)>
<cfloop query="qFiles">
<cfif arguments.overwrite OR NOT FileExists("#dir_to##name#")>
<cffile action="copy" source="#dir_from##name#" destination="#dir_to##name#">
<cfset notifyEvent(
"copyFiles:file",
StructFromArgs(source="#dir_from##name#",destination="#dir_to#")
)>
</cfif>
</cfloop>
<cfset notifyEvent("copyFiles",Arguments)>
</cffunction>
<cffunction name="deleteFile" access="public" returntype="any" output="no" hint="I delete the given file.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset Arguments.destination = getFilePath(argumentCollection=arguments)>
<cfif FileExists(Arguments.destination)>
<cfset FileDelete(Arguments.destination)>
<cfset notifyEvent("deleteFile",Arguments)>
</cfif>
</cffunction>
<cffunction name="getFolderList" output="false" returnType="query">
<cfargument name="Folder" type="string" required="true">
<cfset Arguments.directory = getDirectory(Arguments.Folder)>
<cfreturn getMyDirectoryList(ArgumentCollection=Arguments)>
</cffunction>
<!---
Mimics the cfdirectory, action="list" command.
Updated with final CFMX var code.
Fixed a bug where the filter wouldn't show dirs.
@param directory The directory to list. (Required)
@param filter Optional filter to apply. (Optional)
@param sort Sort to apply. (Optional)
@param recurse Recursive directory list. Defaults to false. (Optional)
@return Returns a query.
@author Raymond Camden ([email protected])
@version 2, April 8, 2004
--->
<cffunction name="getMyDirectoryList" output="false" returnType="query">
<cfargument name="directory" type="string" required="true">
<cfargument name="filter" type="string" required="false" default="">
<cfargument name="sort" type="string" required="false" default="">
<cfargument name="recurse" type="boolean" required="false" default="false">
<!--- temp vars --->
<cfargument name="dirInfo" type="query" required="false">
<cfargument name="thisDir" type="query" required="false">
<!--- more vars --->
<cfargument name="exclude" type="string" default="">
<cfset var delim = variables.dirdelim>
<cfset var ScriptName = 0>
<cfset var isExcluded = false>
<cfset var exdir = false>
<cfset var qDirs = 0>
<cfset var qFiles = 0>
<cfset var cols = "attributes,datelastmodified,mode,name,size,type,directory">
<cfset var sDirectoryAttributes = {name="qFiles",directory="#arguments.directory#",type="file"}>
<cfset var replacer = ReplaceNoCase(Arguments.directory, Variables.UploadPath, "", "ONE")>
<cfif Right(arguments.directory,1) NEQ delim>
<cfset arguments.directory = "#arguments.directory##delim#">
</cfif>
<cfif NOT StructKeyExists(arguments,"dirInfo")>
<cfset arguments.dirInfo = QueryNew(cols)>
</cfif>
<cfif Len(Arguments.sort)>
<cfset sDirectoryAttributes["sort"] = Arguments.sort>
</cfif>
<cfif Len(Arguments.filter)>
<cfset sDirectoryAttributes["filter"] = Arguments.filter>
</cfif>
<cfdirectory attributeCollection="#sDirectoryAttributes#">
<cfif arguments.recurse>
<cfdirectory name="qDirs" directory="#arguments.directory#" sort="#sort#" type="dir">
<cfloop query="qDirs">
<cfif type IS "dir">
<cfif StructKeyExists(variables,"instance") AND StructKeyExists(variables.instance,"RootPath")>
<cfset ScriptName = "/" & ReplaceNoCase(ReplaceNoCase("#arguments.directory##name#",variables.instance.RootPath,""),"\","/","ALL")>
</cfif>
<cfset isExcluded = false>
<cfif Len(arguments.exclude)>
<cfloop list="#arguments.exclude#" index="exdir">
<cfif
( Len(ScriptName) AND ListLen(exdir,"/") EQ 1 AND exdir EQ ListFindNoCase("#ScriptName#/",exdir,"/") )
OR ( Len(ScriptName) AND Len(exdir) AND Left(ScriptName,Len(exdir)) EQ exdir )
OR ( Len(ScriptName) AND Len(exdir) AND Left(exdir,Len(ScriptName)) EQ ScriptName )
OR ( exdir EQ name )
>
<cfset isExcluded = true>
</cfif>
</cfloop>
</cfif>
<cfif NOT isExcluded>
<cfset getMyDirectoryList(directory=directory & name,filter=filter,sort=sort,recurse=true,dirInfo=arguments.dirInfo,exclude=exclude)>
</cfif>
</cfif>
</cfloop>
</cfif>
<cfoutput query="qFiles">
<cfset QueryAddRow(arguments.dirInfo)>
<cfset QuerySetCell(arguments.dirInfo,"attributes",attributes)>
<cfset QuerySetCell(arguments.dirInfo,"datelastmodified",datelastmodified)>
<cfset QuerySetCell(arguments.dirInfo,"mode",mode)>
<cfset QuerySetCell(arguments.dirInfo,"name",ReplaceNoCase(name,replacer,"","ONE"))>
<cfset QuerySetCell(arguments.dirInfo,"size",size)>
<cfset QuerySetCell(arguments.dirInfo,"type",type)>
<cfset QuerySetCell(arguments.dirInfo,"directory",arguments.directory)>
</cfoutput>
<cfreturn arguments.dirInfo>
</cffunction>
<cffunction name="FileNameFromString" access="public" returntype="string" output="no">
<cfargument name="string" type="string" required="yes">
<cfargument name="extensions" type="string" default="cfm,htm,html">
<cfset var exts = arguments.extensions>
<cfset var result = "">
<cfset var ext = ListLast(arguments.string,".")>
<cfif Len(ext) AND ListFindNoCase(exts,ext) AND (Len(arguments.string)-Len(ext)-1) GT 0>
<cfset arguments.string = Left(arguments.string,Len(arguments.string)-Len(ext)-1)>
</cfif>
<cfset result = PathNameFromString(arguments.string)>
<cfif Len(result)>
<cfif Len(ext) AND ListFindNoCase(exts,ext)>
<cfset result = "#result#.#ext#">
<cfelse>
<cfset result = "#result#.#ListFirst(exts)#">
</cfif>
</cfif>
<cfreturn LCase(result)>
</cffunction>
<cffunction name="getDelimiter" access="public" returntype="string" output="no">
<cfreturn variables.dirdelim>
</cffunction>
<cffunction name="getDirectory" access="public" returntype="string" output="no">
<cfargument name="Folder" type="string" required="no">
<cfset var result = variables.UploadPath>
<cfif StructKeyExists(arguments,"Folder")>
<cfset arguments.Folder = convertFolder(arguments.Folder,variables.dirdelim)>
<cfif len(result) gt 1>
<cfif Right(result,1) EQ variables.dirdelim>
<cfset result = Left(result,Len(result)-1)>
</cfif>
</cfif>
<cfif DirectoryExists(arguments.Folder)>
<cfset result = arguments.Folder>
<cfelse>
<cfset result = ListAppend(result,arguments.Folder,variables.dirdelim)>
</cfif>
</cfif>
<cfif Right(result,1) NEQ variables.dirdelim>
<cfset result = "#result##variables.dirdelim#">
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getFileLen" access="public" returntype="numeric" output="no">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var result = 0>
<cfset var path = getDirectory(argumentCollection=arguments)>
<cfset var qFiles = 0>
<cfdirectory action="LIST" directory="#path#" name="qFiles" filter="#arguments.FileName#">
<cfloop query="qFiles">
<cfif Name EQ arguments.FileName>
<cfset result = size>
</cfif>
</cfloop>
<cfreturn result>
</cffunction>
<cffunction name="getFilePath" access="public" returntype="string" output="no">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" default="">
<cfset var result = "">
<cfset var FileName_Clean = cleanFileName(arguments.FileName)>
<cfif ListLen(Arguments.FileName,variables.dirdelim) GT 1 AND FileExists(Arguments.FileName)>
<cfset result = Arguments.FileName>
<cfelse>
<cfset result = getDirectory(arguments.Folder) & arguments.FileName>
<cfif ( arguments.FileName NEQ FileName_Clean ) AND NOT FileExists(result)>
<cfset result = getDirectory(arguments.Folder) & cleanFileName(arguments.FileName)>
</cfif>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getFileURL" access="public" returntype="string" output="no">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var result = variables.UploadURL>
<cfif StructKeyExists(arguments,"Folder")>
<cfset result = ListAppend(result,convertFolder(arguments.Folder,"/"),"/")>
</cfif>
<cfset result = ListAppend(result,arguments.FileName,"/")>
<cfset result = ReplaceNoCase(result,"//","/","ALL")>
<cfset result = ReReplaceNoCase(result,"^(\w+:)/","\1//","ALL")>
<cfreturn result>
</cffunction>
<cffunction name="getUploadPath" access="public" returntype="string" output="no">
<cfreturn variables.UploadPath>
</cffunction>
<cffunction name="getUploadURL" access="public" returntype="string" output="no">
<cfreturn variables.UploadURL>
</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 var parent = "">
<cfif NOT DirectoryExists(Arguments.Directory) AND ListLen(Arguments.Directory,variables.dirdelim)>
<cfset parent = ListDeleteAt(Arguments.Directory,ListLen(Arguments.Directory,variables.dirdelim),variables.dirdelim)>
<cfif NOT DirectoryExists(parent)>
<cfset makedir(parent)>
</cfif>
<cfset makedir_private(Arguments.Directory)>
<cfset notifyEvent("makedir",Arguments)>
</cfif>
</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">
<cfset makedir(getDirectory(Arguments.Folder))>
</cffunction>
<cffunction name="PathNameFromString" access="public" returntype="string" output="false" hint="">
<cfargument name="string" type="string" required="yes">
<cfset var reChars = "([0-9]|[a-z]|[A-Z])">
<cfset var ii = 0>
<cfset var result = "">
<cfloop index="ii" from="1" to="#Len(string)#" step="1">
<cfif REFindNoCase(reChars, Mid(string,ii,1))>
<cfset result = result & Mid(string,ii,1)>
<cfelse>
<cfset result = result & "-">
</cfif>
</cfloop>
<cfset result = REReplaceNoCase(result, "_{2,}", "_", "ALL")>
<cfset result = REReplaceNoCase(result, "-{2,}", "-", "ALL")>
<cfset result = ReplaceNoCase(result,"-"," ","ALL")>
<cfset result = ReplaceNoCase(Trim(result)," ","-","ALL")>
<cfreturn LCase(result)>
</cffunction>
<cffunction name="readBinaryFile" access="public" returntype="binary" output="no" hint="I return the contents of a file.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var destination = getFilePath(argumentCollection=arguments)>
<cfset var result = "">
<cffile action="readBinary" file="#destination#" variable="result">
<cfreturn result>
</cffunction>
<cffunction name="readFile" access="public" returntype="string" output="no" hint="I return the contents of a file.">
<cfargument name="FileName" type="string" required="yes">
<cfargument name="Folder" type="string" required="no">
<cfset var destination = getFilePath(argumentCollection=arguments)>
<cfset var result = "">
<cffile action="READ" file="#destination#" variable="result">
<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 = getFilePath(argumentCollection=arguments)>
<cfif StructKeyExists(arguments,"Folder")>
<cfset makeFolder(arguments.Folder)>
</cfif>
<cffile action="WRITE" file="#destination#" output="#arguments.Contents#" addnewline="no">
<cfset notifyEvent("writeFile",Arguments)>
<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 = getFilePath(argumentCollection=arguments)>
<cfif StructKeyExists(arguments,"Folder")>
<cfset makeFolder(arguments.Folder)>
</cfif>
<cffile action="WRITE" file="#destination#" output="#arguments.Contents#" addnewline="no">
<cfset notifyEvent("writeBinaryFile",Arguments)>
<cfreturn destination>
</cffunction>
<cffunction name="addLine" access="public" returntype="string" output="no" hint="I add a line to 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 result = "">
<cfset var cr = "
">
<cfset Arguments.Contents = Trim(Arguments.Contents)>
<cfif Len(Arguments.Contents)>
<cfset Arguments.Contents = readFile(ArgumentCollection=Arguments) & cr & Arguments.Contents>
<cfset result = writeFile(ArgumentCollection=Arguments)>
<cfelse>
<cfset result = getFilePath(argumentCollection=arguments)>
</cfif>
<cfreturn result>
</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 serverPath = "">
<cfset var dirdelim = getDirDelim()>
<cfset var result = "">
<cfset var UploadFileName = "">
<cfset var sFileArgs = {
action="UPLOAD",
filefield="#Arguments.FieldName#",
NameConflict=Arguments.NameConflict,
result="CFFILE",
destination=destination
}>
<cfif StructKeyExists(Arguments,"accept")>
<cfset sFileArgs["accept"] = Arguments.accept>
</cfif>
<!--- 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>
<cfif StructKeyExists(Form,Arguments.FieldName)>
<cfset UploadFileName = getClientFileName(Arguments.FieldName)>
<cfset UploadFileName = FileNameFromString(UploadFileName,ListLast(UploadFileName,"."))>
<!--- Check file extension --->
<cfif
Len(arguments.extensions)
AND NOT ListFindNoCase(arguments.extensions,ListLast(UploadFileName,"."))
>
<cfreturn StructNew()>
</cfif>
<cfset sFileArgs["destination"] = ListAppend(sFileArgs["destination"],UploadFileName,dirDelim)>
<cfelse>
<cfset sFileArgs["action"] = "COPY">
</cfif>
<!--- Handle the upload --->
<cffile attributeCollection="#sFileArgs#">
<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>
<cfset notifyEvent("uploadFile",Arguments)>
<cfreturn result>
</cffunction>
<cffunction name="convertFolder" access="public" returntype="string" output="no">
<cfargument name="Folder" type="string" required="yes">
<cfargument name="delimiter" type="string" default="/">
<cfset var result = arguments.Folder>
<cfset result = ListChangeDelims(result,"/",",")>
<cfset result = ListChangeDelims(result,variables.dirdelim,"/")>
<cfset result = ListChangeDelims(result,arguments.delimiter,variables.dirdelim)>
<cfreturn result>
</cffunction>
<cffunction name="getFilePrefix" access="public" returntype="string" output="no" hint="I return the file name without the extension.">
<cfargument name="FileName" type="string" required="yes">
<cfreturn Reverse(ListRest(Reverse(getFileFromPath(arguments.FileName)),"."))>
</cffunction>
<!---
Copies a directory.
@param source Source directory. (Required)
@param destination Destination directory. (Required)
@param nameConflict What to do when a conflict occurs (skip, overwrite, makeunique). Defaults to overwrite. (Optional)
@return Returns nothing.
@author Joe Rinehart ([email protected])
@version 1, July 27, 2005
--->
<cffunction name="copyDirectories" output="true">
<cfargument name="source" required="true" type="string">
<cfargument name="destination" required="true" type="string">
<cfargument name="nameconflict" required="true" default="overwrite">
<cfset var contents = "" />
<cfset var dirDelim = getDelimiter()>
<cfif not(directoryExists(arguments.destination))>
<cfdirectory action="create" directory="#arguments.destination#">
</cfif>
<cfdirectory action="list" directory="#arguments.source#" name="contents">
<cfloop query="contents">
<cfif contents.type eq "file">
<cffile action="copy" source="#arguments.source##dirDelim##name#" destination="#arguments.destination##dirDelim##name#" nameconflict="#arguments.nameConflict#">
<cfelseif contents.type eq "dir">
<cfset copyDirectories(arguments.source & dirDelim & name, arguments.destination & dirDelim & name) />
</cfif>
</cfloop>
</cffunction>
<!---
* v2, bug found with dots in path, bug found by joseph
*
* @author Marc Esher ([email protected])
* @version 2, January 22, 2008
--->
<cffunction name="createUniqueFileName" acess="public" returntype="string" output="no" hint="Creates a unique file name; used to prevent overwriting when moving or copying files from one location to another.">
<cfargument name="fullPath" type="string" required="true" hint="Full path to file.">
<cfargument name="maxfilelength" type="numeric" default="0">
<cfscript>
var extension = "";
var thePath = "";
var dir = "";
var filebase = "";
var result = arguments.fullPath;
var counter = 0;
if ( FileExists(Arguments.fullPath) ) {
if( ListLen(fullPath,".") gte 2 ) {
extension = "." & ListLast(fullPath,".");
}
thePath = ListDeleteAt(fullPath,ListLen(fullPath,"."),".");
filebase = ListLast(arguments.fullPath,getDirDelim());
dir = ReReplaceNoCase(arguments.fullPath,"#ReplaceNoCase(filebase,'.','\.','ALL')#$","");
filebase = ListDeleteAt(filebase,ListLen(filebase,"."),".");
if ( arguments.maxfilelength AND Len(filebase & extension) GT arguments.maxfilelength ) {
filebase = Left(filebase,arguments.maxfilelength-extension);
}
while( FileExists(result) ) {
counter = counter + 1;
result = LimitFileNameLength(arguments.maxfilelength,thePath,"_" & counter & extension);
}
}
return result;
</cfscript>
</cffunction>
<cfscript>
/**
* Makes a row of a query into a structure.
*
* @param query The query to work with.
* @param row Row number to check. Defaults to row 1.
* @return Returns a structure.
* @author Nathan Dintenfass ([email protected])
* @version 1, December 11, 2001
*/
</cfscript>
<cffunction name="cleanFileName" access="public" returntype="string" output="false">
<cfargument name="name" type="string" required="yes">
<cfargument name="maxlength" type="numeric" default="0">
<cfset var result = ReReplaceNoCase(arguments.name,"[^a-zA-Z0-9_\-\.]","_","ALL")><!--- Remove special characters from file name --->
<cfset result = ReReplaceNoCase(result,"\s+","_","ALL")><!--- Remove empty space from file name --->
<cfset result = ReReplaceNoCase(result,"_{2,}","_","ALL")>
<cfset result = LimitFileNameLength(arguments.maxlength,result)>
<cfreturn result>
</cffunction>
<cffunction name="unzip" access="public" returntype="any" output="false">
<cfargument name="file" type="string" required="yes">
<cfargument name="destination" type="string" required="yes">
<cfset Arguments.action = "unzip">
<cfzip AttributeCollection="#Arguments#">
<cfset notifyEvent("unzip",Arguments)>
</cffunction>
<cffunction name="zip" access="public" returntype="any" output="false">
<cfargument name="source" type="string" required="yes">
<cfargument name="file" type="string" required="yes">
<cfset Arguments.action = "zip">
<cfzip AttributeCollection="#Arguments#">
<cfset notifyEvent("zip",Arguments)>
</cffunction>
<cffunction name="notifyEvent" access="package" returntype="void" output="false" hint="">
<cfargument name="EventName" type="string" required="true">
<cfargument name="Args" type="struct" required="false">
<cfargument name="result" type="any" required="false">
<cfif StructKeyExists(Variables,"Observer")>
<cfset Arguments.EventName = "FileMgr:#Arguments.eventName#">
<cfset Variables.Observer.notifyEvent(ArgumentCollection=Arguments)>
</cfif>
</cffunction>
<cffunction name="fixFileName" access="private" returntype="string" output="false">
<cfargument name="name" type="string" required="yes">
<cfargument name="dir" type="string" required="yes">
<cfargument name="maxlength" type="numeric" default="0">
<cfset var dirdelim = getDirDelim()>
<cfset var result = cleanFileName(name=Arguments.name,maxlength=Arguments.maxlength)>
<cfset var path = "#dir##dirdelim##result#">
<!--- If corrected file name doesn't match original, rename it --->
<cfif arguments.name NEQ result AND FileExists("#arguments.dir##dirdelim##arguments.name#")>
<cfset path = createUniqueFileName(path,arguments.maxlength)>
<cfset result = ListLast(path,dirdelim)>
<cffile action="rename" source="#arguments.dir##dirdelim##arguments.name#" destination="#result#">
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="StructFromArgs" access="public" returntype="struct" output="false" hint="">
<cfset var sTemp = 0>
<cfset var sResult = StructNew()>
<cfset var key = "">
<cfif ArrayLen(arguments) EQ 1 AND isStruct(arguments[1])>
<cfset sTemp = arguments[1]>
<cfelse>
<cfset sTemp = arguments>
</cfif>
<!--- set all arguments into the return struct --->
<cfloop collection="#sTemp#" item="key">
<cfif StructKeyExists(sTemp, key)>
<cfset sResult[key] = sTemp[key]>
</cfif>
</cfloop>
<cfreturn sResult>
</cffunction>
<cfscript>
//http://www.stillnetstudios.com/get-filename-before-calling-cffile/
function getClientFileName(fieldName) {
var loc = {};
loc.aParts = Form.getPartsArray();
loc.part = "";
if ( StructKeyExists(loc,"aParts") ) {
for (loc.part in loc.aParts) {
if ( loc.part.isFile() AND loc.part.getName() EQ Arguments.fieldName ) {
return loc.part.getFileName();
}
}
}
//Railo code.
loc.sHere = GetPageContext();
if ( StructKeyExists(loc.sHere,"formScope") ) {
loc.sHere = loc.sHere.formScope();
if ( StructKeyExists(loc.sHere,"getUploadResource") ) {
loc.sHere = loc.sHere.getUploadResource(Arguments.fieldName);
if ( StructKeyExists(loc.sHere,"getName") ) {
return loc.sHere.getName();
}
}
}
//GetPageContext().formScope().getUploadResource("myFormField").getName()
return "";
}
</cfscript>
</cfcomponent>