From 583571a77b32d45e2252224e0c5d43c89523b7bb Mon Sep 17 00:00:00 2001 From: shijy-naruto Date: Mon, 29 Apr 2013 18:12:38 +0800 Subject: [PATCH] add crc32 for upload --- Demo/Demo.cs | 590 ++++++++++++++++++++++---------------------- QBox/QBox.csproj | 181 +++++++------- QBox/RS/RSClient.cs | 168 +++++++------ QBox/Util/CRC32.cs | 81 ++++++ 4 files changed, 562 insertions(+), 458 deletions(-) create mode 100644 QBox/Util/CRC32.cs diff --git a/Demo/Demo.cs b/Demo/Demo.cs index 7c9d5703..3513a186 100644 --- a/Demo/Demo.cs +++ b/Demo/Demo.cs @@ -1,290 +1,300 @@ -using System; -using QBox.Auth; -using QBox.RS; -using QBox.FileOp; -using QBox.RPC; - -namespace QBox.Demo -{ - public class Demo - { - public static string bucketName; - public static string key; - public static string localFile; - public static string bigkey; - public static string bigFile; - public static string DEMO_DOMAIN; - public static Client conn; - public static RSService rs; - - public static void Main() - { - Config.ACCESS_KEY = ""; - Config.SECRET_KEY = ""; - - bucketName = "yourbucket"; - DEMO_DOMAIN = bucketName + ".qiniudn.com"; - key = "gogopher.jpg"; - localFile = "Resource/gogopher.jpg"; - bigkey = key; - bigFile = localFile; - - conn = new DigestAuthClient(); - rs = new RSService(conn, bucketName); - - MkBucket(); - RSClientPutFile(); - Get(key); - ResumablePutFile(); - Stat(bigkey); - Delete(key); - Drop(); - - MkBucket(); - RSPutFile(); - ImageOps(); - - MakeDownloadToken(); - - Console.ReadLine(); - } - - public static void MkBucket() - { - Console.WriteLine("\n===> RSService.MkBucket"); - CallRet callRet = rs.MkBucket(); - PrintRet(callRet); - } - - public static void RSPutFile() - { - Console.WriteLine("\n===> RSService.PutFile"); - PutFileRet putFileRet = rs.PutFile(key, null, localFile, null); - PrintRet(putFileRet); - if (putFileRet.OK) - { - Console.WriteLine("Hash: " + putFileRet.Hash); - } - else - { - Console.WriteLine("Failed to PutFile"); - } - } - - public static void RSClientPutFile() - { - Console.WriteLine("\n===> RSClient Generate UpToken"); - var authPolicy = new AuthPolicy(bucketName, 3600); - string upToken = authPolicy.MakeAuthTokenString(); - Console.WriteLine("upToken: " + upToken); - - Console.WriteLine("\n===> RSClient.PutFileWithUpToken"); - PutFileRet putFileRet = RSClient.PutFileWithUpToken(upToken, bucketName, key, null, localFile, null, "key="); - PrintRet(putFileRet); - if (putFileRet.OK) - { - Console.WriteLine("Hash: " + putFileRet.Hash); - } - else - { - Console.WriteLine("Failed to RSClient.PutFileWithUpToken"); - } - } - - public static void ResumablePutFile() - { - Console.WriteLine("\n===> ResumablePut.PutFile"); - var authPolicy = new AuthPolicy(bucketName, 3600); - string upToken = authPolicy.MakeAuthTokenString(); - PutAuthClient client = new PutAuthClient(upToken); - PutFileRet putFileRet = ResumablePut.PutFile(client, bucketName, bigkey, null, bigFile, null, "key="); - PrintRet(putFileRet); - if (putFileRet.OK) - { - Console.WriteLine("Hash: " + putFileRet.Hash); - } - else - { - Console.WriteLine("Failed to ResumablePut.PutFile"); - } - } - - public static void Get(string key) - { - Console.WriteLine("\n===> RSService.Get"); - GetRet getRet = rs.Get(key, "attName"); - PrintRet(getRet); - if (getRet.OK) - { - Console.WriteLine("Hash: " + getRet.Hash); - Console.WriteLine("FileSize: " + getRet.FileSize); - Console.WriteLine("MimeType: " + getRet.MimeType); - Console.WriteLine("Url: " + getRet.Url); - } - else - { - Console.WriteLine("Failed to Get"); - } - - Console.WriteLine("\n===> RSService.GetIfNotModified"); - getRet = rs.GetIfNotModified(key, "attName", getRet.Hash); - PrintRet(getRet); - if (getRet.OK) - { - Console.WriteLine("Hash: " + getRet.Hash); - Console.WriteLine("FileSize: " + getRet.FileSize); - Console.WriteLine("MimeType: " + getRet.MimeType); - Console.WriteLine("Url: " + getRet.Url); - } - else - { - Console.WriteLine("Failed to GetIfNotModified"); - } - } - - public static void Stat(string key) - { - Console.WriteLine("\n===> RSService.Stat"); - StatRet statRet = rs.Stat(key); - PrintRet(statRet); - if (statRet.OK) - { - Console.WriteLine("Hash: " + statRet.Hash); - Console.WriteLine("FileSize: " + statRet.FileSize); - Console.WriteLine("PutTime: " + statRet.PutTime); - Console.WriteLine("MimeType: " + statRet.MimeType); - } - else - { - Console.WriteLine("Failed to Stat"); - } - } - - public static void Delete(string key) - { - Console.WriteLine("\n===> RSService.Delete"); - CallRet deleteRet = rs.Delete(key); - PrintRet(deleteRet); - if (!deleteRet.OK) - { - Console.WriteLine("Failed to Delete"); - } - } - - public static void Drop() - { - Console.WriteLine("\n===> RSService.Drop"); - CallRet dropRet = rs.Drop(); - PrintRet(dropRet); - if (!dropRet.OK) - { - Console.WriteLine("Failed to Drop"); - } - } - - public static void MakeDownloadToken() - { - Console.WriteLine("\n===> Auth.MakeDownloadToken"); - string pattern = "*/*"; - var downloadPolicy = new DownloadPolicy(pattern, 3600); - string dnToken = downloadPolicy.MakeAuthTokenString(); - Console.WriteLine("dnToken: " + dnToken); - } - - public static void ImageOps() - { - Console.WriteLine("\n===> FileOp.ImageInfo"); - ImageInfoRet infoRet = ImageOp.ImageInfo("http://" + DEMO_DOMAIN + "/" + key); - PrintRet(infoRet); - if (infoRet.OK) - { - Console.WriteLine("Format: " + infoRet.Format); - Console.WriteLine("Width: " + infoRet.Width); - Console.WriteLine("Heigth: " + infoRet.Height); - Console.WriteLine("ColorModel: " + infoRet.ColorModel); - } - else - { - Console.WriteLine("Failed to ImageInfo"); - } - - Console.WriteLine("\n===> FileOp.ImageExif"); - CallRet exifRet = ImageOp.ImageExif("http://" + DEMO_DOMAIN + "/" + key); - PrintRet(exifRet); - if (!exifRet.OK) - { - Console.WriteLine("Failed to ImageExif"); - } - - Console.WriteLine("\n===> FileOp.ImageViewUrl"); - ImageViewSpec viewSpec = new ImageViewSpec{Mode = 0, Width = 200, Height= 200}; - string viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); - Console.WriteLine("ImageViewUrl 1:" + viewUrl); - viewSpec.Quality = 1; - viewSpec.Format = "gif"; - viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); - Console.WriteLine("ImageViewUrl 2:" + viewUrl); - viewSpec.Quality = 90; - viewSpec.Sharpen = 10; - viewSpec.Format = "png"; - viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); - Console.WriteLine("ImageViewUrl 3:" + viewUrl); - - Console.WriteLine("\n===> FileOp.ImageMogrifyUrl"); - ImageMogrifySpec mogrSpec = new ImageMogrifySpec { - Thumbnail = "!50x50r", Gravity = "center", Rotate = 90, - Crop = "!50x50", Quality = 80, AutoOrient = true - }; - string mogrUrl = ImageOp.ImageMogrifyUrl("http://" + DEMO_DOMAIN + "/" + key, mogrSpec); - Console.WriteLine("ImageMogrifyUrl:" + mogrUrl); - - Console.WriteLine("\n===> Get"); - GetRet getRet = rs.Get(key, "save-as"); - PrintRet(getRet); - if (getRet.OK) - { - Console.WriteLine("Hash: " + getRet.Hash); - Console.WriteLine("FileSize: " + getRet.FileSize); - Console.WriteLine("MimeType: " + getRet.MimeType); - Console.WriteLine("Url: " + getRet.Url); - } - else - { - Console.WriteLine("Failed to Get"); - } - Console.WriteLine("\n===> FileOp.ImageMogrifySaveAs"); - PutFileRet saveAsRet = rs.ImageMogrifySaveAs(getRet.Url, mogrSpec, key + ".mogr-save-as"); - PrintRet(saveAsRet); - if (saveAsRet.OK) - { - Console.WriteLine("Hash: " + saveAsRet.Hash); - } - else - { - Console.WriteLine("Failed to ImageMogrifySaveAs"); - } - Console.WriteLine("\n===> Get"); - getRet = rs.Get(key + ".mogr-save-as", "mogr-save-as.jpg"); - PrintRet(getRet); - if (getRet.OK) - { - Console.WriteLine("Hash: " + getRet.Hash); - Console.WriteLine("FileSize: " + getRet.FileSize); - Console.WriteLine("MimeType: " + getRet.MimeType); - Console.WriteLine("Url: " + getRet.Url); - } - else - { - Console.WriteLine("Failed to Get"); - } - } - - public static void PrintRet(CallRet callRet) - { - Console.WriteLine("\n[CallRet]"); - Console.WriteLine("StatusCode: " + callRet.StatusCode.ToString()); - Console.WriteLine("Response:\n" + callRet.Response); - Console.WriteLine(); - } - } -} +using System; +using QBox.Auth; +using QBox.RS; +using QBox.FileOp; +using QBox.RPC; +using QBox.Util; + +namespace QBox.Demo +{ + public class Demo + { + public static string bucketName; + public static string key; + public static string localFile; + public static string bigkey; + public static string bigFile; + public static string DEMO_DOMAIN; + public static Client conn; + public static RSService rs; + + public static void Main() + { + Config.ACCESS_KEY = ""; + Config.SECRET_KEY = ""; + + bucketName = "yourbucket"; + DEMO_DOMAIN = bucketName + ".qiniudn.com"; + key = "gogopher.jpg"; + localFile = "Resource/gogopher.jpg"; + bigkey = key; + bigFile = localFile; + + conn = new DigestAuthClient(); + rs = new RSService(conn, bucketName); + + MkBucket(); + RSClientPutFile(); + RSClientPutFileWithCRC32(); + Get(key); + ResumablePutFile(); + Stat(bigkey); + Delete(key); + Drop(); + + MkBucket(); + ImageOps(); + + MakeDownloadToken(); + + Console.ReadLine(); + } + + public static void MkBucket() + { + Console.WriteLine("\n===> RSService.MkBucket"); + CallRet callRet = rs.MkBucket(); + PrintRet(callRet); + } + + public static void RSClientPutFile() + { + Console.WriteLine("\n===> RSClient Generate UpToken"); + var authPolicy = new AuthPolicy(bucketName, 3600); + string upToken = authPolicy.MakeAuthTokenString(); + Console.WriteLine("upToken: " + upToken); + + Console.WriteLine("\n===> RSClient.PutFileWithUpToken"); + PutFileRet putFileRet = RSClient.PutFileWithUpToken(upToken, bucketName, key, null, localFile, null, "key="); + PrintRet(putFileRet); + if (putFileRet.OK) + { + Console.WriteLine("Hash: " + putFileRet.Hash); + } + else + { + Console.WriteLine("Failed to RSClient.PutFileWithUpToken"); + } + } + + public static void RSClientPutFileWithCRC32() + { + Console.WriteLine("\n===> RSClientPutFileWithCRC32 Generate CRC32"); + UInt32 crc = CRC32.CheckSumFile(localFile); + Console.WriteLine("CRC32: " + crc.ToString()); + + Console.WriteLine("\n===> RSClientPutFileWithCRC32 Generate UpToken"); + var authPolicy = new AuthPolicy(bucketName, 3600); + string upToken = authPolicy.MakeAuthTokenString(); + Console.WriteLine("upToken: " + upToken); + + Console.WriteLine("\n===> RSClient.PutFileWithUpToken(CRC32)"); + PutFileRet putFileRet = RSClient.PutFileWithUpToken(upToken, bucketName, key, null, localFile, null, "key=", crc); + PrintRet(putFileRet); + if (putFileRet.OK) + { + Console.WriteLine("Hash: " + putFileRet.Hash); + } + else + { + Console.WriteLine("Failed to RSClient.PutFileWithUpToken(CRC32)"); + } + } + + public static void ResumablePutFile() + { + Console.WriteLine("\n===> ResumablePut.PutFile"); + var authPolicy = new AuthPolicy(bucketName, 3600); + string upToken = authPolicy.MakeAuthTokenString(); + PutAuthClient client = new PutAuthClient(upToken); + PutFileRet putFileRet = ResumablePut.PutFile(client, bucketName, bigkey, null, bigFile, null, "key="); + PrintRet(putFileRet); + if (putFileRet.OK) + { + Console.WriteLine("Hash: " + putFileRet.Hash); + } + else + { + Console.WriteLine("Failed to ResumablePut.PutFile"); + } + } + + public static void Get(string key) + { + Console.WriteLine("\n===> RSService.Get"); + GetRet getRet = rs.Get(key, "attName"); + PrintRet(getRet); + if (getRet.OK) + { + Console.WriteLine("Hash: " + getRet.Hash); + Console.WriteLine("FileSize: " + getRet.FileSize); + Console.WriteLine("MimeType: " + getRet.MimeType); + Console.WriteLine("Url: " + getRet.Url); + } + else + { + Console.WriteLine("Failed to Get"); + } + + Console.WriteLine("\n===> RSService.GetIfNotModified"); + getRet = rs.GetIfNotModified(key, "attName", getRet.Hash); + PrintRet(getRet); + if (getRet.OK) + { + Console.WriteLine("Hash: " + getRet.Hash); + Console.WriteLine("FileSize: " + getRet.FileSize); + Console.WriteLine("MimeType: " + getRet.MimeType); + Console.WriteLine("Url: " + getRet.Url); + } + else + { + Console.WriteLine("Failed to GetIfNotModified"); + } + } + + public static void Stat(string key) + { + Console.WriteLine("\n===> RSService.Stat"); + StatRet statRet = rs.Stat(key); + PrintRet(statRet); + if (statRet.OK) + { + Console.WriteLine("Hash: " + statRet.Hash); + Console.WriteLine("FileSize: " + statRet.FileSize); + Console.WriteLine("PutTime: " + statRet.PutTime); + Console.WriteLine("MimeType: " + statRet.MimeType); + } + else + { + Console.WriteLine("Failed to Stat"); + } + } + + public static void Delete(string key) + { + Console.WriteLine("\n===> RSService.Delete"); + CallRet deleteRet = rs.Delete(key); + PrintRet(deleteRet); + if (!deleteRet.OK) + { + Console.WriteLine("Failed to Delete"); + } + } + + public static void Drop() + { + Console.WriteLine("\n===> RSService.Drop"); + CallRet dropRet = rs.Drop(); + PrintRet(dropRet); + if (!dropRet.OK) + { + Console.WriteLine("Failed to Drop"); + } + } + + public static void MakeDownloadToken() + { + Console.WriteLine("\n===> Auth.MakeDownloadToken"); + string pattern = "*/*"; + var downloadPolicy = new DownloadPolicy(pattern, 3600); + string dnToken = downloadPolicy.MakeAuthTokenString(); + Console.WriteLine("dnToken: " + dnToken); + } + + public static void ImageOps() + { + Console.WriteLine("\n===> FileOp.ImageInfo"); + ImageInfoRet infoRet = ImageOp.ImageInfo("http://" + DEMO_DOMAIN + "/" + key); + PrintRet(infoRet); + if (infoRet.OK) + { + Console.WriteLine("Format: " + infoRet.Format); + Console.WriteLine("Width: " + infoRet.Width); + Console.WriteLine("Heigth: " + infoRet.Height); + Console.WriteLine("ColorModel: " + infoRet.ColorModel); + } + else + { + Console.WriteLine("Failed to ImageInfo"); + } + + Console.WriteLine("\n===> FileOp.ImageExif"); + CallRet exifRet = ImageOp.ImageExif("http://" + DEMO_DOMAIN + "/" + key); + PrintRet(exifRet); + if (!exifRet.OK) + { + Console.WriteLine("Failed to ImageExif"); + } + + Console.WriteLine("\n===> FileOp.ImageViewUrl"); + ImageViewSpec viewSpec = new ImageViewSpec{Mode = 0, Width = 200, Height= 200}; + string viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); + Console.WriteLine("ImageViewUrl 1:" + viewUrl); + viewSpec.Quality = 1; + viewSpec.Format = "gif"; + viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); + Console.WriteLine("ImageViewUrl 2:" + viewUrl); + viewSpec.Quality = 90; + viewSpec.Sharpen = 10; + viewSpec.Format = "png"; + viewUrl = ImageOp.ImageViewUrl("http://" + DEMO_DOMAIN + "/" + key, viewSpec); + Console.WriteLine("ImageViewUrl 3:" + viewUrl); + + Console.WriteLine("\n===> FileOp.ImageMogrifyUrl"); + ImageMogrifySpec mogrSpec = new ImageMogrifySpec { + Thumbnail = "!50x50r", Gravity = "center", Rotate = 90, + Crop = "!50x50", Quality = 80, AutoOrient = true + }; + string mogrUrl = ImageOp.ImageMogrifyUrl("http://" + DEMO_DOMAIN + "/" + key, mogrSpec); + Console.WriteLine("ImageMogrifyUrl:" + mogrUrl); + + Console.WriteLine("\n===> Get"); + GetRet getRet = rs.Get(key, "save-as"); + PrintRet(getRet); + if (getRet.OK) + { + Console.WriteLine("Hash: " + getRet.Hash); + Console.WriteLine("FileSize: " + getRet.FileSize); + Console.WriteLine("MimeType: " + getRet.MimeType); + Console.WriteLine("Url: " + getRet.Url); + } + else + { + Console.WriteLine("Failed to Get"); + } + Console.WriteLine("\n===> FileOp.ImageMogrifySaveAs"); + PutFileRet saveAsRet = rs.ImageMogrifySaveAs(getRet.Url, mogrSpec, key + ".mogr-save-as"); + PrintRet(saveAsRet); + if (saveAsRet.OK) + { + Console.WriteLine("Hash: " + saveAsRet.Hash); + } + else + { + Console.WriteLine("Failed to ImageMogrifySaveAs"); + } + Console.WriteLine("\n===> Get"); + getRet = rs.Get(key + ".mogr-save-as", "mogr-save-as.jpg"); + PrintRet(getRet); + if (getRet.OK) + { + Console.WriteLine("Hash: " + getRet.Hash); + Console.WriteLine("FileSize: " + getRet.FileSize); + Console.WriteLine("MimeType: " + getRet.MimeType); + Console.WriteLine("Url: " + getRet.Url); + } + else + { + Console.WriteLine("Failed to Get"); + } + } + + public static void PrintRet(CallRet callRet) + { + Console.WriteLine("\n[CallRet]"); + Console.WriteLine("StatusCode: " + callRet.StatusCode.ToString()); + Console.WriteLine("Response:\n" + callRet.Response); + Console.WriteLine(); + } + } +} diff --git a/QBox/QBox.csproj b/QBox/QBox.csproj index a71da270..8f48cd55 100644 --- a/QBox/QBox.csproj +++ b/QBox/QBox.csproj @@ -1,91 +1,92 @@ - - - - Debug - x86 - 8.0.30703 - 2.0 - {1C8C9909-57ED-44E4-81A5-0904E96FA00E} - Library - Properties - QBox - QBox - v4.0 - - - 512 - - - x86 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - x86 - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + Debug + x86 + 8.0.30703 + 2.0 + {1C8C9909-57ED-44E4-81A5-0904E96FA00E} + Library + Properties + QBox + QBox + v4.0 + + + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/QBox/RS/RSClient.cs b/QBox/RS/RSClient.cs index 67aa293c..e08a6c3b 100644 --- a/QBox/RS/RSClient.cs +++ b/QBox/RS/RSClient.cs @@ -1,78 +1,90 @@ -using System; -using System.Collections.Generic; -using System.Net; -using QBox.Util; -using QBox.RPC; - -namespace QBox.RS -{ - public class RSClient - { - public static PutFileRet PutFile( - string url, string bucketName, string key, string mimeType, - string localFile, string customMeta, string callbackParam) - { - string entryURI = bucketName + ":" + key; - if (String.IsNullOrEmpty(mimeType)) - { - mimeType = "application/octet-stream"; - } - string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) + - "/mimeType/" + Base64UrlSafe.Encode(mimeType); - if (!String.IsNullOrEmpty(customMeta)) - { - action += "/meta/" + Base64UrlSafe.Encode(customMeta); - } - - try - { - var postParams = new Dictionary(); - postParams["action"] = action; - postParams["file"] = new FileParameter(localFile, mimeType); - if (!String.IsNullOrEmpty(callbackParam)) - postParams["params"] = callbackParam; - CallRet callRet = MultiPartFormDataPost.Post(url, postParams); - return new PutFileRet(callRet); - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - return new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)); - } - } - - public static PutFileRet PutFileWithUpToken( - string upToken, string tableName, string key, string mimeType, - string localFile, string customMeta, string callbackParam) - { - string entryURI = tableName + ":" + key; - if (String.IsNullOrEmpty(mimeType)) - { - mimeType = "application/octet-stream"; - } - string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) + - "/mimeType/" + Base64UrlSafe.Encode(mimeType); - if (!String.IsNullOrEmpty(customMeta)) - { - action += "/meta/" + Base64UrlSafe.Encode(customMeta); - } - - try - { - var postParams = new Dictionary(); - postParams["auth"] = upToken; - postParams["action"] = action; - postParams["file"] = new FileParameter(localFile, mimeType); - if (!String.IsNullOrEmpty(callbackParam)) - postParams["params"] = callbackParam; - CallRet callRet = MultiPartFormDataPost.Post(Config.UP_HOST + "/upload", postParams); - return new PutFileRet(callRet); - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - return new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)); - } - } - } -} +using System; +using System.Collections.Generic; +using System.Net; +using QBox.Util; +using QBox.RPC; + +namespace QBox.RS +{ + public class RSClient + { + public static PutFileRet PutFile( + string url, string bucketName, string key, string mimeType, + string localFile, string customMeta, string callbackParam) + { + string entryURI = bucketName + ":" + key; + if (String.IsNullOrEmpty(mimeType)) + { + mimeType = "application/octet-stream"; + } + string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) + + "/mimeType/" + Base64UrlSafe.Encode(mimeType); + if (!String.IsNullOrEmpty(customMeta)) + { + action += "/meta/" + Base64UrlSafe.Encode(customMeta); + } + + try + { + var postParams = new Dictionary(); + postParams["action"] = action; + postParams["file"] = new FileParameter(localFile, mimeType); + if (!String.IsNullOrEmpty(callbackParam)) + postParams["params"] = callbackParam; + CallRet callRet = MultiPartFormDataPost.Post(url, postParams); + return new PutFileRet(callRet); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + return new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)); + } + } + + public static PutFileRet PutFileWithUpToken( + string upToken, string tableName, string key, string mimeType, + string localFile, string customMeta, string callbackParam) + { + return PutFileWithUpToken(upToken, tableName, key, + mimeType, localFile, customMeta, callbackParam, 0); + } + + public static PutFileRet PutFileWithUpToken( + string upToken, string tableName, string key, string mimeType, + string localFile, string customMeta, string callbackParam, UInt32 crc32) + { + string entryURI = tableName + ":" + key; + if (String.IsNullOrEmpty(mimeType)) + { + mimeType = "application/octet-stream"; + } + string action = "/rs-put/" + Base64UrlSafe.Encode(entryURI) + + "/mimeType/" + Base64UrlSafe.Encode(mimeType); + if (!String.IsNullOrEmpty(customMeta)) + { + action += "/meta/" + Base64UrlSafe.Encode(customMeta); + } + if (crc32 != 0) + { + action += "/crc32/" + crc32.ToString(); + } + + try + { + var postParams = new Dictionary(); + postParams["auth"] = upToken; + postParams["action"] = action; + postParams["file"] = new FileParameter(localFile, mimeType); + if (!String.IsNullOrEmpty(callbackParam)) + postParams["params"] = callbackParam; + CallRet callRet = MultiPartFormDataPost.Post(Config.UP_HOST + "/upload", postParams); + return new PutFileRet(callRet); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + return new PutFileRet(new CallRet(HttpStatusCode.BadRequest, e)); + } + } + } +} diff --git a/QBox/Util/CRC32.cs b/QBox/Util/CRC32.cs new file mode 100644 index 00000000..5cc0b064 --- /dev/null +++ b/QBox/Util/CRC32.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; + +namespace QBox.Util +{ + public class CRC32 + { + public const UInt32 IEEE = 0xedb88320; + + private UInt32[] Table; + private UInt32 Value; + + public CRC32() + { + Value = 0; + Table = MakeTable(IEEE); + } + + public void Write(byte[] p, int offset, int count) + { + this.Value = Update(this.Value, this.Table, p, offset, count); + } + + public UInt32 Sum32() + { + return this.Value; + } + + private static UInt32[] MakeTable(UInt32 poly) + { + UInt32[] table = new UInt32[256]; + for (int i = 0; i < 256; i++) + { + UInt32 crc = (UInt32)i; + for (int j = 0; j < 8; j++) + { + if ((crc & 1) == 1) + crc = (crc >> 1) ^ poly; + else + crc >>= 1; + } + table[i] = crc; + } + return table; + } + + public static UInt32 Update(UInt32 crc, UInt32[] table, byte[] p, int offset, int count) + { + crc = ~crc; + for (int i = 0; i < count; i++) + { + crc = table[((byte)crc) ^ p[offset+i]] ^ (crc >> 8); + } + return ~crc; + } + + public static UInt32 CheckSumBytes(byte[] data) + { + CRC32 crc = new CRC32(); + crc.Write(data, 0, data.Length); + return crc.Sum32(); + } + + public static UInt32 CheckSumFile(string fileName) + { + CRC32 crc = new CRC32(); + int bufferLen = 32*1024; + using (FileStream fs = File.OpenRead(fileName)) + { + byte[] buffer = new byte[bufferLen]; + while (true) + { + int n = fs.Read(buffer, 0, bufferLen); + if (n == 0) break; + crc.Write(buffer, 0, n); + } + } + return crc.Sum32(); + } + } +}