Skip to content

Commit 48810fd

Browse files
author
Will Spaetzel
committed
Update to latest version from CastRoller
1 parent 9f0573e commit 48810fd

40 files changed

+1760
-1734
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
*.dll
2+
*.pdb
3+
*.suo
4+
*.pidb
5+
*.csproj.user
6+
*.userprefs
7+
*.Publish.xml
8+
*.tmproj
9+
.DS_Store
10+
*.cache
11+
bin
12+
Bin
13+
App_Data
14+
App_data
15+
*~
16+
*FileListAbsolute.txt
17+
combined_*.css
18+
combined_*.js
19+
CastThree/*/combined*
20+
CastRollerEF/migrate/*.sql
21+
*/obj/*
22+
ThumbGen/download/*
23+
ThumbGen/generate/*
24+
*/bin/*

AWSAuthConnection.cs

100644100755
+443-443
Large diffs are not rendered by default.

AmazonS3.cs

100644100755
+171-171
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,171 @@
1-
using System;
2-
using System.IO;
3-
using Spaetzel.UtilityLibrary;
4-
5-
namespace com.CastRoller.AmazonS3DA
6-
{
7-
/// <summary>
8-
/// A class that provides simple static methods for performing common operations on Amazon S3
9-
/// </summary>
10-
public class AmazonS3
11-
{
12-
/// <summary>
13-
/// Returns an authenticated connection to Amazon S3
14-
/// </summary>
15-
/// <returns></returns>
16-
public static AWSAuthConnection GetConnection()
17-
{
18-
return new AWSAuthConnection( Config.AwsAccessKey, Config.AwsSecretKey );
19-
}
20-
21-
/// <summary>
22-
/// Copies a file from the local filesystem to the specified bucket
23-
/// </summary>
24-
/// <param name="bucket">The bucket on s3 the file should be coppied to</param>
25-
/// <param name="key">The name that should be used on s3 for the file</param>
26-
/// <param name="filePath">The full path to the file on the local filesystem</param>
27-
/// <returns>An S3 response for the results of putting the file to S3</returns>
28-
public static Response PutFile( string bucket, string key, string filePath, bool setPublic )
29-
{
30-
31-
32-
// Open the file for streaming
33-
Stream dis = File.OpenRead(filePath);
34-
35-
36-
37-
38-
return PutStream(bucket, key, dis, setPublic);
39-
}
40-
41-
public static Response PutStream(string bucket, string key, Stream stream, bool setPublic )
42-
{
43-
S3StreamObject s3Object = new S3StreamObject(stream, null);
44-
45-
int lastSlash = Math.Max(key.LastIndexOf("/"), key.LastIndexOf("\\"));
46-
47-
48-
string fileName = key.Substring(lastSlash + 1);
49-
50-
string extension = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
51-
52-
53-
System.Collections.SortedList headers = new System.Collections.SortedList();
54-
55-
bool setDisposition = false;
56-
57-
if (extension == "exe" || extension == "bin")
58-
{
59-
headers["Content-Type"] = "application/octet-stream";
60-
setDisposition = true;
61-
}
62-
else if (extension == "zip")
63-
{
64-
headers["Content-Type"] = "application/zip";
65-
setDisposition = true;
66-
}
67-
else if (extension == "tar")
68-
{
69-
headers["Content-Type"] = "application/x-tar";
70-
setDisposition = true;
71-
}
72-
else if (extension == "gz")
73-
{
74-
headers["Content-Type"] = "application/x-gzip";
75-
setDisposition = true;
76-
}
77-
else if (extension == "png")
78-
{
79-
headers["Content-Type"] = "image/png";
80-
}
81-
else if (extension == "gif")
82-
{
83-
headers["Content-Type"] = "image/gif";
84-
}
85-
else if (extension == "jpg" || extension == "jpeg" )
86-
{
87-
headers["Content-Type"] = "image/jpeg";
88-
89-
}
90-
91-
if (setDisposition)
92-
{
93-
headers["Content-Disposition"] = "attachment; filename=\"" + fileName + "\"";
94-
}
95-
96-
try
97-
{
98-
99-
100-
// Get a connection to s3
101-
AWSAuthConnection conn = AmazonS3.GetConnection();
102-
103-
104-
// Stream the file to S3
105-
Response response = conn.putStream(bucket, key, s3Object, headers);
106-
107-
if (setPublic)
108-
{
109-
string acl = "<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID><DisplayName>amazon</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"Group\"><URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission></Grant><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\"><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID></Grantee><Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>";
110-
conn.putACL(bucket, key, acl, null);
111-
}
112-
113-
return response;
114-
}
115-
catch
116-
{
117-
// Probably couldn't connect ro s3
118-
return null;
119-
}
120-
}
121-
122-
/// <summary>
123-
/// Deletes the given file from Amazon S3
124-
/// </summary>
125-
/// <param name="bucket">The bucket to delete from</param>
126-
/// <param name="key">The key to delete</param>
127-
/// <returns>An S3 response with the results of deleting the file</returns>
128-
public static Response DeleteFile( string bucket, string key )
129-
{
130-
131-
// Get a connection
132-
AWSAuthConnection conn = AmazonS3.GetConnection();
133-
134-
// Actually delete the file
135-
Response response = conn.delete(bucket, key, null );
136-
137-
return response;
138-
}
139-
140-
/// <summary>
141-
/// Generates a query string authenticated URL for accessing the specified file on S3
142-
/// See here for information on query string authentication: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/S3_QSAuth.html
143-
/// </summary>
144-
/// <param name="bucket">The bucket the file is contained in</param>
145-
/// <param name="key">The key for the file to get a download link to</param>
146-
/// <param name="expires">The DateTime that the download link should expire</param>
147-
/// <returns>An absolute URL to download the requested file</returns>
148-
public static string GetDownloadLink( string bucket, string key, DateTime expires )
149-
{
150-
// Set up the generator with the AWS login information
151-
QueryStringAuthGenerator generator = new QueryStringAuthGenerator( Config.AwsAccessKey, Config.AwsSecretKey, false );
152-
153-
// Set the expiry date (Unix timestamp in milliseconds )
154-
generator.Expires = (long)Utilities.ConvertToUnixTimestamp( expires )*1000;
155-
156-
// Get the download link
157-
string link = generator.get( bucket, key, null );
158-
159-
// If the bucket is a subdomain of castroller.com, change the URL to point to
160-
// That subdomain instead of amazon
161-
if( bucket.IndexOf( "castroller.com" ) > 0 )
162-
{
163-
string remove = "s3.amazonaws.com:80/";
164-
link = link.Remove( link.IndexOf(remove), remove.Length );
165-
}
166-
167-
return link;
168-
169-
}
170-
}
171-
}
1+
using System;
2+
using System.IO;
3+
using Spaetzel.UtilityLibrary;
4+
5+
namespace com.CastRoller.AmazonS3DA
6+
{
7+
/// <summary>
8+
/// A class that provides simple static methods for performing common operations on Amazon S3
9+
/// </summary>
10+
public class AmazonS3
11+
{
12+
/// <summary>
13+
/// Returns an authenticated connection to Amazon S3
14+
/// </summary>
15+
/// <returns></returns>
16+
public static AWSAuthConnection GetConnection()
17+
{
18+
return new AWSAuthConnection( Config.AwsAccessKey, Config.AwsSecretKey );
19+
}
20+
21+
/// <summary>
22+
/// Copies a file from the local filesystem to the specified bucket
23+
/// </summary>
24+
/// <param name="bucket">The bucket on s3 the file should be coppied to</param>
25+
/// <param name="key">The name that should be used on s3 for the file</param>
26+
/// <param name="filePath">The full path to the file on the local filesystem</param>
27+
/// <returns>An S3 response for the results of putting the file to S3</returns>
28+
public static Response PutFile( string bucket, string key, string filePath, bool setPublic )
29+
{
30+
31+
32+
// Open the file for streaming
33+
Stream dis = File.OpenRead(filePath);
34+
35+
36+
37+
38+
return PutStream(bucket, key, dis, setPublic);
39+
}
40+
41+
public static Response PutStream(string bucket, string key, Stream stream, bool setPublic )
42+
{
43+
S3StreamObject s3Object = new S3StreamObject(stream, null);
44+
45+
int lastSlash = Math.Max(key.LastIndexOf("/"), key.LastIndexOf("\\"));
46+
47+
48+
string fileName = key.Substring(lastSlash + 1);
49+
50+
string extension = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
51+
52+
53+
System.Collections.SortedList headers = new System.Collections.SortedList();
54+
55+
bool setDisposition = false;
56+
57+
if (extension == "exe" || extension == "bin")
58+
{
59+
headers["Content-Type"] = "application/octet-stream";
60+
setDisposition = true;
61+
}
62+
else if (extension == "zip")
63+
{
64+
headers["Content-Type"] = "application/zip";
65+
setDisposition = true;
66+
}
67+
else if (extension == "tar")
68+
{
69+
headers["Content-Type"] = "application/x-tar";
70+
setDisposition = true;
71+
}
72+
else if (extension == "gz")
73+
{
74+
headers["Content-Type"] = "application/x-gzip";
75+
setDisposition = true;
76+
}
77+
else if (extension == "png")
78+
{
79+
headers["Content-Type"] = "image/png";
80+
}
81+
else if (extension == "gif")
82+
{
83+
headers["Content-Type"] = "image/gif";
84+
}
85+
else if (extension == "jpg" || extension == "jpeg" )
86+
{
87+
headers["Content-Type"] = "image/jpeg";
88+
89+
}
90+
91+
if (setDisposition)
92+
{
93+
headers["Content-Disposition"] = "attachment; filename=\"" + fileName + "\"";
94+
}
95+
96+
try
97+
{
98+
99+
100+
// Get a connection to s3
101+
AWSAuthConnection conn = AmazonS3.GetConnection();
102+
103+
104+
// Stream the file to S3
105+
Response response = conn.putStream(bucket, key, s3Object, headers);
106+
107+
if (setPublic)
108+
{
109+
string acl = "<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID><DisplayName>amazon</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"Group\"><URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission></Grant><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\"><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID></Grantee><Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>";
110+
conn.putACL(bucket, key, acl, null);
111+
}
112+
113+
return response;
114+
}
115+
catch
116+
{
117+
// Probably couldn't connect ro s3
118+
return null;
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Deletes the given file from Amazon S3
124+
/// </summary>
125+
/// <param name="bucket">The bucket to delete from</param>
126+
/// <param name="key">The key to delete</param>
127+
/// <returns>An S3 response with the results of deleting the file</returns>
128+
public static Response DeleteFile( string bucket, string key )
129+
{
130+
131+
// Get a connection
132+
AWSAuthConnection conn = AmazonS3.GetConnection();
133+
134+
// Actually delete the file
135+
Response response = conn.delete(bucket, key, null );
136+
137+
return response;
138+
}
139+
140+
/// <summary>
141+
/// Generates a query string authenticated URL for accessing the specified file on S3
142+
/// See here for information on query string authentication: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/S3_QSAuth.html
143+
/// </summary>
144+
/// <param name="bucket">The bucket the file is contained in</param>
145+
/// <param name="key">The key for the file to get a download link to</param>
146+
/// <param name="expires">The DateTime that the download link should expire</param>
147+
/// <returns>An absolute URL to download the requested file</returns>
148+
public static string GetDownloadLink( string bucket, string key, DateTime expires )
149+
{
150+
// Set up the generator with the AWS login information
151+
QueryStringAuthGenerator generator = new QueryStringAuthGenerator( Config.AwsAccessKey, Config.AwsSecretKey, false );
152+
153+
// Set the expiry date (Unix timestamp in milliseconds )
154+
generator.Expires = (long)Utilities.ConvertToUnixTimestamp( expires )*1000;
155+
156+
// Get the download link
157+
string link = generator.get( bucket, key, null );
158+
159+
// If the bucket is a subdomain of castroller.com, change the URL to point to
160+
// That subdomain instead of amazon
161+
if( bucket.IndexOf( "castroller.com" ) > 0 )
162+
{
163+
string remove = "s3.amazonaws.com:80/";
164+
link = link.Remove( link.IndexOf(remove), remove.Length );
165+
}
166+
167+
return link;
168+
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)