forked from coldbox-modules/s3sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFMLFacade.cfc
180 lines (141 loc) · 5.9 KB
/
CFMLFacade.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
component accessors="true"{
// ********************************************************************************
// Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
// www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com
// ********************************************************************************
// CFML Facade to provide function equivaliency for S3 Operations
property name="SDK" inject="AmazonS3@s3sdk";
property name="S3Bucket";
property name="BucketURL";
public function init( required string bucketName ){
VARIABLES.s3Bucket = ARGUMENTS.bucketName;
VARIABLES.tmpDir = expandPath( "/includes/tmp" );
//protocol-agnostic public URL
VARIABLES.BucketURL = "//" & arguments.bucketName & ".s3.amazonaws.com";
if( !directoryExists( VARIABLES.tmpDir ) ){
directoryCreate( VARIABLES.tmpDir );
}
return this;
}
public query function directoryList(
required string path,
filter="",
sort=""
){
ARGUMENTS.path = urlEncodedFormat( arguments.path );
if( left( ARGUMENTS.path, 1 ) == '/' ){
ARGUMENTS.path = right( ARGUMENTS.path, len( ARGUMENTS.path ) - 1 );
}
if( right( ARGUMENTS.path, 1 ) == '/' ){
ARGUMENTS.path = left( ARGUMENTS.path, len( ARGUMENTS.path ) - 1 );
}
var s3Listing = SDK.getBucket( VARIABLES.s3Bucket, ARGUMENTS.path );
var qDirectory = queryNew( "name,size,type,dateLastModified,attributes,mode,directory" );
for( var item in s3Listing ){
if( item.key != ( urlDecode( ARGUMENTS.path ) & "/" ) && item.key != ( ARGUMENTS.path & "/" ) ){
var row = queryAddRow( qDirectory, 1 );
var fileName = listLast( item.key, "/");
var directory = VARIABLES.getDirectoryFromPath( item.key );
querySetCell( qDirectory, "name", fileName, row );
querySetCell( qDirectory, "directory", directory, row );
querySetCell( qDirectory, "dateLastModified", parseDateTime( item.lastModified ), row );
querySetCell( qDirectory, "type", item.isDirectory ? "Dir" : "File", row );
querySetCell( qDirectory, "size", item.size, row );
querySetCell( qDirectory, "attributes", item.etag, row );
}
}
if( len( ARGUMENTS.filter ) ){
var qFilter = new query();
qFilter.setDBType( "query" );
qFilter.setAttributes( qDirectory=qDirectory );
var sql = "SELECT * from qDirectory WHERE name LIKE :filter";
qFilter.addParam( "filter", replace( ARGUMENTS.filter, "*", "%", "ALL" ) );
qDirectory = qFilter.execute( sql=sql ).getResult();
}
if( len( ARGUMENTS.sort ) ){
var qSort = new query();
qSort.setDBType( "query" );
qSort.setAttributes( qDirectory=qDirectory );
qDirectory = qSort.execute( sql="SELECT * from qDirectory ORDER BY #ARGUMENTS.sort#" )
.getResult();
}
return qDirectory;
}
public boolean function directoryExists( path ){
var dirName = listLast( arguments.path, "/" );
var tlListing = VARIABLES.directoryList( VARIABLES.getDirectoryFromPath( arguments.path ) );
var q = new query();
q.setDBType( "query" );
var sql = "SELECT TOP 1 * as existing from tlListing WHERE type='Dir' AND name=:dirname";
q.addParam( name="dirname", value=dirName, cfsqltype="cf_sql_varchar" );
q.setAttributes( tlListing=tlListing );
var qExists = q.execute( sql=sql ).getResult();
return javacast( "boolean", qExists.recordcount );
}
public string function getDirectoryFromPath( path ){
var tlPathArray = listToArray( arguments.path, "/" );
var dirName = tlPathArray[ arraylen( tlPathArray ) ];
arrayDeleteAt( tlPathArray, arraylen( tlPathArray ) );
var tlPath = arrayToList( tlPathArray, "/" );
return tlPath;
}
public function directoryCreate( path ){
SDK.putObjectFolder( VARIABLES.s3Bucket, urlEncodedFormat( ARGUMENTS.path ) & "/" );
}
public function directoryDelete( path, recurse=false ){
var dirName = listLast( arguments.path, "/" );
var dirListing = VARIABLES.directoryList( VARIABLES.getDirectoryFromPath( urlEncodedFormat( arguments.path ) ) );
for( var row in dirListing ){
if( row.type == "Dir" && row.name == dirName ){
return SDK.deleteObject( VARIABLES.s3Bucket, SDK.deleteObject( VARIABLES.s3Bucket, urlEncodedFormat( row.directory ) & "/" & urlEncodedFormat( row.name ) & "/" ) );
}
}
throw( "The directory #arguments.path# does not exist in the S3 Bucket #VARIABLES.S3Bucket#" );
}
public any function getFileInfo( path ){
var bFile = fileRead( "http://" & VARIABLES.s3Bucket & ".s3.amazonaws.com/" & arguments.path );
if( len( bFile.toString() ) && !isXML( bfile.toString() ) ){
var S3Info = SDK.getObjectInfo( VARIABLES.s3Bucket, ARGUMENTS.path );
return S3Info;
}
}
public function fileExists( path ){
return !isNull( VARIABLES.getFileInfo( urlEncodedFormat( path ) ) );
}
public function fileRead( path ){
return fileRead( getBucketURL() & "/" & urlEncodedFormat( arguments.path ) );
}
public function fileReadBinary( path ){
return fileReadBinary( getBucketURL() & "/" & urlEncodedFormat( arguments.path ) );
}
public function fileWrite( fileObj ,path ){
if( fileExists( fileObj ) ){
fileObj = fileReadBinary( fileObj );
}
return SDK.putObject( VARIABLES.s3Bucket, urlEncodedFormat( arguments.path ), fileObj );
}
public function fileSetAccessMode( path, mode ){
var acl = SDK.ACL_PRIVATE;
switch( right( arguments.mode.toString(), 1 ) ){
case "7":
case "6":
acl = SDK.ACL_PUBLIC_READ_WRITE;
break;
case "5":
acl = SDK.ACL_PUBLIC_READ;
break;
case "0":
acl = SDK.ACL_AUTH_READ;
break;
default:
break;
}
return SDK.copyObject( VARIABLES.s3Bucket, arguments.path, VARIABLES.s3Bucket, arguments.path, acl )
}
public function fileMove( currentPath, targetPath ){
return SDK.renameObject( VARIABLES.s3Bucket, urlEncodedFormat( ARGUMENTS.currentPath ), VARIABLES.s3Bucket, urlEncodedFormat( ARGUMENTS.targetPath ) );
}
public function fileDelete( filepath ){
return SDK.deleteObject( VARIABLES.s3Bucket, urlEncodedFormat( arguments.filepath ) );
}
}