本文整理汇总了C#中DB.Download方法的典型用法代码示例。如果您正苦于以下问题:C# DB.Download方法的具体用法?C# DB.Download怎么用?C# DB.Download使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DB
的用法示例。
在下文中一共展示了DB.Download方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToDisk
public static void WriteToDisk (this DBWorkFile wf, DB db, string dir)
{
byte [] buffer = new byte [1024];
int read;
string filename = Path.Combine (dir, wf.filename);
DBFile file = DBFile_Extensions.Create (db, wf.file_id);
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
using (Stream stream = db.Download (wf)) {
using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Read)) {
while (0 != (read = stream.Read (buffer, 0, buffer.Length))) {
fs.Write (buffer, 0, read);
}
}
}
if (file.compressed_mime == "application/x-gzip")
FileUtilities.GZUncompress (filename);
}
示例2: CompressFiles
public static int CompressFiles ()
{
byte [] buffer = new byte [1024];
int read;
long saved_space = 0;
using (DB db = new DB (true)) {
using (DB db_save = new DB (true)) {
using (IDbCommand cmd = db.CreateCommand ()) {
cmd.CommandText = @"
SELECT File.*
FROM File
WHERE (File.compressed_mime = '' OR File.compressed_mime IS NULL)
AND File.size <> 0
AND File.id IN
(SELECT WorkFile.file_id FROM WorkFile WHERE WorkFile.file_id = File.id)
LIMIT 10
";
using (IDataReader reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
DBFile file = new DBFile (reader);
long srclength;
long destlength = -1;
string tmpfile = Path.GetTempFileName ();
string tmpfilegz;
Console.Write ("Downloading {0} = {1} with size {2}... ", file.id, file.filename, file.size);
using (Stream stream_reader = db_save.Download (file)) {
using (FileStream stream_writer = new FileStream (tmpfile, FileMode.Create, FileAccess.Write)) {
while (0 < (read = stream_reader.Read (buffer, 0, buffer.Length))) {
stream_writer.Write (buffer, 0, read);
}
}
}
srclength = new FileInfo (tmpfile).Length;
Console.Write ("Compressing file {0} with size {1}... ", tmpfile, srclength);
tmpfilegz = FileUtilities.GZCompress (tmpfile);
if (tmpfilegz == null) {
Console.WriteLine ("Compression didn't succeed.");
} else {
destlength = new FileInfo (tmpfilegz).Length;
Console.WriteLine ("Success, compressed size: {0} ({1}%)", destlength, 100 * (double) destlength / (double) srclength);
using (IDbTransaction transaction = db_save.BeginTransaction ()) {
// Upload the compressed file.
// Npgsql doesn't seem to have a way to truncate a large object,
// so we just create a new large object and delete the old one.
int file_id = file.file_id.Value;
int gzfile_id = db_save.Manager.Create (LargeObjectManager.READWRITE);
LargeObject gzfile = db_save.Manager.Open (gzfile_id, LargeObjectManager.READWRITE);
using (FileStream st = new FileStream (tmpfilegz, FileMode.Open, FileAccess.Read, FileShare.Read)) {
while (0 < (read = st.Read (buffer, 0, buffer.Length)))
gzfile.Write (buffer, 0, read);
}
gzfile.Close ();
// Save to our File record
file.file_id = gzfile_id;
file.compressed_mime = "application/x-gzip";
file.Save (db_save);
// Delete the old large object
db_save.Manager.Delete (file_id);
transaction.Commit ();
saved_space += (srclength - destlength);
}
}
if (File.Exists (tmpfilegz)) {
try {
File.Delete (tmpfilegz);
} catch {
// Ignore
}
}
if (File.Exists (tmpfile)) {
try {
File.Delete (tmpfile);
} catch {
// Ignore
}
}
}
}
//}
}
}
}
Console.WriteLine ("Saved {0} bytes.", saved_space);
return 0;
}
示例3: MoveFilesToFileSystem
public static int MoveFilesToFileSystem ()
{
long moved_bytes = 0;
Manager.log.Info ("MoveFilesToFileSystem: [START]");
using (DB db = new DB ()) {
using (DB download_db = new DB ()) {
while (true) {
using (IDbCommand cmd = db.CreateCommand ()) {
// execute this in chunks to avoid huge data transfers and slowdowns.
cmd.CommandText = "SELECT * FROM File WHERE NOT file_id IS NULL LIMIT 100";
using (IDataReader reader = cmd.ExecuteReader ()) {
if (!reader.Read ())
break;
do {
DBFile file = new DBFile (reader);
byte [] buffer = new byte [1024];
int oid = file.file_id.Value;
int read;
string fn = FileUtilities.CreateFilename (file.md5, file.compressed_mime == MimeTypes.GZ, true);
using (FileStream writer = new FileStream (fn, FileMode.Create, FileAccess.Write, FileShare.Read)) {
using (Stream str = download_db.Download (file)) {
while ((read = str.Read (buffer, 0, buffer.Length)) != 0)
writer.Write (buffer, 0, read);
}
}
IDbTransaction transaction = download_db.BeginTransaction ();
download_db.Manager.Delete (oid);
file.file_id = null;
file.Save (download_db);
transaction.Commit ();
moved_bytes += file.size;
log.InfoFormat ("MoveFilesToFileSystem: Moved oid {0} to {1} ({2} bytes, {3} total bytes moved)", oid, fn, file.size, moved_bytes);
} while (reader.Read ());
}
}
}
while (true) {
using (IDbCommand cmd = db.CreateCommand ()) {
// execute this in chunks to avoid huge data transfers and slowdowns.
cmd.CommandText = "SELECT * FROM Revision WHERE (diff_file_id IS NULL AND NOT diff = '') OR (log_file_id IS NULL AND NOT log = '') LIMIT 100";
using (IDataReader reader = cmd.ExecuteReader ()) {
if (!reader.Read ())
break;
do {
DBRevision revision = new DBRevision (reader);
string tmpfile = null;
if (!string.IsNullOrEmpty (revision.diff)) {
int length = 0;
if (revision.diff_file_id == null) {
try {
length = revision.diff.Length;
tmpfile = Path.GetTempFileName ();
File.WriteAllText (tmpfile, revision.diff);
DBFile diff = download_db.Upload (tmpfile, ".log", false, null);
revision.diff_file_id = diff.id;
revision.diff = null;
} finally {
try {
if (File.Exists (tmpfile))
File.Delete (tmpfile);
} catch (Exception ex) {
log.ErrorFormat ("error deleting temp file: {0}", ex);
}
}
moved_bytes += length;
log.InfoFormat ("MoveFilesToFileSystem: Moved revision {0}'s diff to db/filesystem ({1} bytes, {2} total bytes moved)", revision.id, length, moved_bytes);
}
}
if (!string.IsNullOrEmpty (revision.log)) {
int length = 0;
if (revision.log_file_id == null) {
try {
length = revision.log.Length;
tmpfile = Path.GetTempFileName ();
File.WriteAllText (tmpfile, revision.log);
DBFile log = download_db.Upload (tmpfile, ".log", false, null);
revision.log_file_id = log.id;
revision.log = null;
} finally {
try {
if (File.Exists (tmpfile))
File.Delete (tmpfile);
} catch (Exception ex) {
log.ErrorFormat ("error deleting temp file: {0}", ex);
}
}
moved_bytes += length;
Manager.log.InfoFormat ("MoveFilesToFileSystem: Moved revision {0}'s log to db/filesystem ({1} bytes, {2} total bytes moved)", revision.id, length, moved_bytes);
}
revision.log = null;
}
//.........这里部分代码省略.........
示例4: DownloadWorkFile
private void DownloadWorkFile (int workfile_id, string md5)
{
DBWorkFileView view = null;
DBFile file = null;
string filename;
string mime;
string compressed_mime;
using (DB db = new DB ()) {
WebServiceLogin login = Authentication.CreateLogin (Request);
filename = Request ["filename"];
if (!string.IsNullOrEmpty (md5)) { // md5 lookup needs admin rights
Authentication.VerifyUserInRole (Context, db, login, Roles.Administrator, false);
file = DBFile_Extensions.Find (db, md5);
if (file == null)
throw new HttpException (404, "Could not find the file.");
mime = file.mime;
filename = file.filename;
compressed_mime = file.compressed_mime;
} else {
view = DBWorkFileView_Extensions.Find (db, workfile_id);
if (view == null)
throw new HttpException (404, "Could not find the file.");
if ([email protected]) // internal files need admin rights
Authentication.VerifyUserInRole (Context, db, login, Roles.Administrator, false);
else
Authentication.VerifyAnonymousAccess (Context, db, login);
if (!string.IsNullOrEmpty (filename)) {
file = DBWork_Extensions.GetFile (db, view.work_id, filename, false);
if (file == null)
throw new HttpException (404, string.Format ("Could not find the filename '{0}'", filename));
mime = file.mime;
compressed_mime = file.compressed_mime;
md5 = file.md5;
view = null;
} else {
mime = view.mime;
filename = view.filename;
compressed_mime = view.compressed_mime;
}
}
Response.ContentType = mime;
Response.AppendHeader ("Content-Disposition", "filename=\"" + Path.GetFileName (filename) + "\"");
// any access rights verified, serve the file
if (view != null) {
if (view.file_file_id == null) {
DownloadMd5 (view.md5);
} else {
using (Stream str = db.Download (view)) {
DownloadStream (str, compressed_mime);
}
}
} else {
DownloadFile (db, file);
}
}
}
示例5: DownloadFile
private void DownloadFile (DB db, DBFile file)
{
// access must be verified before calling this method (no verification is done here)
if (file.file_id == null) {
DownloadMd5 (file.md5);
} else {
using (Stream str = db.Download (file)) {
DownloadStream (str, file.compressed_mime);
}
}
}