public static string GZCompressManaged(string filename) { string outfile = null; try { outfile = Path.GetTempFileName(); using (FileStream infs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream outfs = new FileStream(outfile, FileMode.Open, FileAccess.Write, FileShare.Read)) { using (GZipStream gz = new GZipStream(outfs, CompressionMode.Compress)) { byte [] buffer = new byte [1024]; int bytes_read; while ((bytes_read = infs.Read(buffer, 0, buffer.Length)) > 0) { gz.Write(buffer, 0, bytes_read); } } } } return(outfile); } catch (Exception ex) { Logger.Log("Failed to compress file: {0} {1}", filename, ex); FileUtilities.TryDeleteFile(outfile); return(null); } }
public DBFile Upload(string md5, string path_to_contents, string filename, string extension, bool hidden, string compressed_mime) { IDbTransaction transaction = null; LargeObjectManager manager; LargeObject obj; int? oid; DBFile result; long filesize; string gzFilename = null; try { filesize = new FileInfo(path_to_contents).Length; if (filesize > 1024 * 1024 * 500) { throw new Exception("Max file size is 500 MB"); } using (IDbCommand cmd = CreateCommand()) { cmd.CommandText = "SELECT * FROM File WHERE md5 = '" + md5 + "'"; using (IDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { return(new DBFile(reader)); } } } //Console.WriteLine ("Uploading {0} {1} with compressed mime: {2}", Filename, md5, compressed_mime); // The file is not in the database // Note: there is a race condition here, // the same file might get added to the db before we do it here. // not quite sure how to deal with that except retrying the above if the insert below fails. if (compressed_mime == MimeTypes.GZ) { gzFilename = path_to_contents; } else { gzFilename = FileUtilities.GZCompress(path_to_contents); compressed_mime = MimeTypes.GZ; } transaction = BeginTransaction(); if (Configuration.StoreFilesInDB) { manager = new LargeObjectManager(this.dbcon); oid = manager.Create(LargeObjectManager.READWRITE); obj = manager.Open(oid.Value, LargeObjectManager.READWRITE); using (FileStream st = new FileStream(gzFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte [] buffer = new byte [1024]; int read = -1; while (read != 0) { read = st.Read(buffer, 0, buffer.Length); obj.Write(buffer, 0, read); } } obj.Close(); } else { oid = null; string fn = FileUtilities.CreateFilename(md5, true, true); File.Copy(gzFilename, fn, true); log.DebugFormat("Saved file to: {0}", fn); } result = new DBFile(); result.file_id = oid; result.filename = Path.GetFileName(filename); result.md5 = md5; result.size = (int)filesize; result.hidden = hidden; switch (extension.ToLower()) { case ".log": case ".stdout": case ".stderr": result.mime = MimeTypes.LOG; break; case ".txt": result.mime = MimeTypes.TXT; break; case ".htm": case ".html": result.mime = MimeTypes.HTML; break; case ".png": result.mime = MimeTypes.PNG; break; case ".jpg": result.mime = MimeTypes.JPG; break; case ".bmp": result.mime = MimeTypes.BMP; break; case ".tar": result.mime = MimeTypes.TAR; break; case ".bz": result.mime = MimeTypes.BZ; break; case ".bz2": result.mime = MimeTypes.BZ2; break; case ".zip": result.mime = MimeTypes.ZIP;; break; case ".gz": result.mime = MimeTypes.GZ; break; case ".xpi": result.mime = MimeTypes.XPI; break; case ".crx": result.mime = MimeTypes.CRX; break; default: result.mime = MimeTypes.OCTET_STREAM; break; } result.compressed_mime = compressed_mime; result.Save(this); transaction.Commit(); transaction = null; return(result); } finally { FileUtilities.TryDeleteFile(gzFilename); if (transaction != null) { transaction.Rollback(); } } }