/// <summary> /// Return thumb with given id, type /// </summary> /// <param name="id">image id</param> /// <param name="type">image type</param> /// <param name="ratio">new image ratio</param> /// <returns>resize image body inside stream</returns> private object GetThumb(int type, int id, string ratio) { Response response; string contentType; ratio = ratio.Replace(',', '.'); if (!float.TryParse(ratio, NumberStyles.AllowDecimalPoint, CultureInfo.CreateSpecificCulture("en-EN"), out float newratio)) { newratio = 0.6667f; } string path = GetImagePath(type, id, false); if (string.IsNullOrEmpty(path)) { Stream image = MissingImage(); contentType = "image/png"; response = Response.FromStream(image, contentType); } else { FileStream fs = File.OpenRead(path); contentType = MimeTypes.GetMimeType(path); System.Drawing.Image im = System.Drawing.Image.FromStream(fs); response = Response.FromStream(ResizeImageToRatio(im, newratio), contentType); } return(response); }
private byte[] ReadExactly(string path, int offset, int size) { //if the file length is less than offset+size then always just return //the whole file. sometimes this will mean prehash is a bit //longer to run than with just 1KB designed for but still a trivial //time frame. Done this way for simplicity. FileInfo file = new FileInfo(path); if (file.Length <= (offset + size)) { return(File.ReadAllBytes(path)); } using (System.IO.Stream stream = File.OpenRead(path)) { stream.Position = offset; byte[] buffer = new byte[size]; int bytesRead = 0; do { bytesRead += stream.Read(buffer, bytesRead, size); size -= bytesRead; } while (size > 0); return(buffer); } }
public void TestReplaceIgnoreMerge() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, null, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); } }
public void TestReplaceIgnoreMerge() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { try { fileStream.WriteByte(42); } catch (Exception) { File.Delete(tempLongPathFilename); throw; } } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { try { fileStream.WriteByte(52); } catch (Exception) { File.Delete(tempLongPathFilename2); throw; } } var fi = new FileInfo(tempLongPathFilename); try { const bool ignoreMetadataErrors = true; var fi2 = fi.Replace(tempLongPathFilename2, null, ignoreMetadataErrors); Assert.IsNotNull(fi2); Assert.AreEqual(tempLongPathFilename2, fi2.FullName); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); } }
public void TestReplaceIgnoreMergeWithReadonlyBackupPath() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupPathName = new StringBuilder(longPathDirectory).Append(@"\readonly").ToString(); var di = new DirectoryInfo(tempBackupPathName); di.Create(); var attr = di.Attributes; di.Attributes = attr | FileAttributes.ReadOnly; var tempBackupLongPathFilename = new StringBuilder(tempBackupPathName).Append(@"\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempBackupLongPathFilename)); } finally { di.Attributes = attr; if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); File.Delete(tempBackupLongPathFilename); if (Directory.Exists(tempBackupPathName)) { Directory.Delete(tempBackupPathName); } } }
public static (string e2dk, string crc32, string md5, string sha1) GetHash(string filename) { StringBuilder sb = new StringBuilder(); Native.rhash_library_init(); IntPtr ctx = Native.rhash_init(RHashIds.RHASH_ED2K | RHashIds.RHASH_CRC32 | RHashIds.RHASH_MD5 | RHashIds.RHASH_SHA1); string e2dk = "", crc32 = "", md5 = "", sha1 = ""; using (Stream source = File.OpenRead(filename)) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { var buf = Marshal.AllocHGlobal(bytesRead); Marshal.Copy(buffer, 0, buf, bytesRead); Native.rhash_update(ctx, buf, bytesRead); Marshal.FreeHGlobal(buf); } } IntPtr output = Marshal.AllocHGlobal(200); Native.rhash_print(output, ctx, RHashIds.RHASH_ED2K, RhashPrintSumFlags.RHPR_DEFAULT); e2dk = Marshal.PtrToStringAnsi(output); Native.rhash_print(output, ctx, RHashIds.RHASH_CRC32, RhashPrintSumFlags.RHPR_DEFAULT); crc32 = Marshal.PtrToStringAnsi(output); Native.rhash_print(output, ctx, RHashIds.RHASH_MD5, RhashPrintSumFlags.RHPR_DEFAULT); md5 = Marshal.PtrToStringAnsi(output); Native.rhash_print(output, ctx, RHashIds.RHASH_SHA1, RhashPrintSumFlags.RHPR_DEFAULT); sha1 = Marshal.PtrToStringAnsi(output); Marshal.FreeHGlobal(output); Native.rhash_final(ctx, IntPtr.Zero); Native.rhash_free(ctx); return(e2dk, crc32, md5, sha1); }
public void TestOpenRead() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (var stream = File.OpenRead(tempLongPathFilename)) { Assert.AreEqual(42, stream.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
/// <summary> /// Return image with given id, type /// </summary> /// <param name="id">image id</param> /// <param name="type">image type</param> /// <returns>image body inside stream</returns> private object GetImage(int type, int id) { Response response; string contentType; string path = GetImagePath(type, id, false); if (string.IsNullOrEmpty(path)) { Stream image = MissingImage(); contentType = "image/png"; response = Response.FromStream(image, contentType); } else { FileStream fs = File.OpenRead(path); contentType = MimeTypes.GetMimeType(path); response = Response.FromStream(fs, contentType); } return(response); }
public void TestReplaceIgnoreMergeWithInvalidBackupPath() { Assert.Throws <DirectoryNotFoundException>(() => { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\gibberish\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const bool ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempBackupLongPathFilename)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); File.Delete(tempBackupLongPathFilename); } }); }
public DataFile(string n, string[] t, string y, string s, string f, string ft = "") { name = n; tags = t; type = y; subject = s; fileName = f; if (ft == "") { fullText = ""; Stream fileStream = File.OpenRead(GetPDFPath()); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream); foreach (PdfPageBase page in loadedDocument.Pages) { fullText += page.ExtractText(true).ToLower() + " "; } loadedDocument.Close(); File.WriteAllText(GetFullTextPath(), fullText); } else { fullText = ft; } }
/// <summary> /// Opens the base stream, handling sFTP access /// </summary> /// <param name="path">The path.</param> /// <param name="encryptedPassphraseFunc">The encrypted passphrase function.</param> /// <returns> /// An improved stream where the base stream is set /// </returns> private static ImprovedStream OpenBaseStream(string path, Func <string> encryptedPassphraseFunc) { var retVal = new ImprovedStream { AssumePGP = path.AssumePgp(), AssumeGZip = path.AssumeGZip(), }; if (retVal.AssumePGP && encryptedPassphraseFunc != null) { retVal.EncryptedPassphrase = encryptedPassphraseFunc(); } try { retVal.BasePath = path; retVal.BaseStream = File.OpenRead(path); return(retVal); } catch (Exception) { retVal.Close(); throw; } }
public Stream GetFile(string path) { return(File.OpenRead(path)); }
public static Media Convert(string filename) { int ex = 0; MediaInfo mi = new MediaInfo(); if (mi == null) { return(null); } if (!File.Exists(filename)) { return(null); } try { mi.Open(filename); ex = 1; Media m = new Media(); Part p = new Part(); Stream VideoStream = null; int video_count = mi.GetInt(StreamKind.General, 0, "VideoCount"); int audio_count = mi.GetInt(StreamKind.General, 0, "AudioCount"); int text_count = mi.GetInt(StreamKind.General, 0, "TextCount"); m.Duration = p.Duration = mi.Get(StreamKind.General, 0, "Duration"); m.Container = p.Container = TranslateContainer(mi.Get(StreamKind.General, 0, "Format")); string codid = mi.Get(StreamKind.General, 0, "CodecID"); if (!string.IsNullOrEmpty(codid) && (codid.Trim().ToLower() == "qt")) { m.Container = p.Container = "mov"; } int brate = mi.GetInt(StreamKind.General, 0, "BitRate"); if (brate != 0) { m.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture); } p.Size = mi.Get(StreamKind.General, 0, "FileSize"); //m.Id = p.Id = mi.Get(StreamKind.General, 0, "UniqueID"); ex = 2; List <Stream> streams = new List <Stream>(); int iidx = 0; if (video_count > 0) { for (int x = 0; x < video_count; x++) { Stream s = TranslateVideoStream(mi, x); if (x == 0) { VideoStream = s; m.Width = s.Width; m.Height = s.Height; if (!string.IsNullOrEmpty(m.Height)) { if (!string.IsNullOrEmpty(m.Width)) { m.VideoResolution = GetResolution(float.Parse(m.Width), float.Parse(m.Height)); m.AspectRatio = GetAspectRatio(float.Parse(m.Width), float.Parse(m.Height), s.PA); } } if (!string.IsNullOrEmpty(s.FrameRate)) { float fr = System.Convert.ToSingle(s.FrameRate); m.VideoFrameRate = ((int)Math.Round(fr)).ToString(CultureInfo.InvariantCulture); if (!string.IsNullOrEmpty(s.ScanType)) { if (s.ScanType.ToLower().Contains("int")) { m.VideoFrameRate += "i"; } else { m.VideoFrameRate += "p"; } } else { m.VideoFrameRate += "p"; } if ((m.VideoFrameRate == "25p") || (m.VideoFrameRate == "25i")) { m.VideoFrameRate = "PAL"; } else if ((m.VideoFrameRate == "30p") || (m.VideoFrameRate == "30i")) { m.VideoFrameRate = "NTSC"; } } m.VideoCodec = s.Codec; if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration)) { double sdur = 0; double mdur = 0; double.TryParse(s.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out sdur); double.TryParse(m.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out mdur); if (sdur > mdur) { m.Duration = p.Duration = ((int)sdur).ToString(); } } if (video_count == 1) { s.Default = null; s.Forced = null; } } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } streams.Add(s); } } ex = 3; int totalsoundrate = 0; if (audio_count > 0) { for (int x = 0; x < audio_count; x++) { Stream s = TranslateAudioStream(mi, x); if ((s.Codec == "adpcm") && (p.Container == "flv")) { s.Codec = "adpcm_swf"; } if (x == 0) { m.AudioCodec = s.Codec; m.AudioChannels = s.Channels; if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration)) { double sdur = 0; double mdur = 0; double.TryParse(s.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out sdur); double.TryParse(m.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out mdur); if (sdur > mdur) { m.Duration = p.Duration = ((int)sdur).ToString(); } } if (audio_count == 1) { s.Default = null; s.Forced = null; } } if (!string.IsNullOrEmpty(s.Bitrate)) { double birate = 0; double.TryParse(s.Bitrate, NumberStyles.Any, CultureInfo.InvariantCulture, out birate); totalsoundrate += (int)brate; } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } streams.Add(s); } } if ((VideoStream != null) && string.IsNullOrEmpty(VideoStream.Bitrate) && !string.IsNullOrEmpty(m.Bitrate)) { double mrate = 0; double.TryParse(m.Bitrate, NumberStyles.Any, CultureInfo.InvariantCulture, out mrate); VideoStream.Bitrate = (((int)mrate) - totalsoundrate).ToString(CultureInfo.InvariantCulture); } ex = 4; if (text_count > 0) { for (int x = 0; x < audio_count; x++) { Stream s = TranslateTextStream(mi, x); streams.Add(s); if (text_count == 1) { s.Default = null; s.Forced = null; } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } } } ex = 5; m.Parts = new List <Part>(); m.Parts.Add(p); bool over = false; if (m.Container == "mkv") { int val = int.MaxValue; foreach (Stream s in streams) { if (string.IsNullOrEmpty(s.Index)) { over = true; break; } s.idx = int.Parse(s.Index); if (s.idx < val) { val = s.idx; } } if ((val != 0) && !over) { foreach (Stream s in streams) { s.idx = s.idx - val; s.Index = s.idx.ToString(CultureInfo.InvariantCulture); } } else if (over) { int xx = 0; foreach (Stream s in streams) { s.idx = xx++; s.Index = s.idx.ToString(CultureInfo.InvariantCulture); } } streams = streams.OrderBy(a => a.idx).ToList(); } ex = 6; p.Streams = streams; if ((p.Container == "mp4") || (p.Container == "mov")) { p.Has64bitOffsets = "0"; p.OptimizedForStreaming = "0"; m.OptimizedForStreaming = "0"; byte[] buffer = new byte[8]; FileStream fs = File.OpenRead(filename); fs.Read(buffer, 0, 4); int siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; fs.Seek(siz, SeekOrigin.Begin); fs.Read(buffer, 0, 8); if ((buffer[4] == 'f') && (buffer[5] == 'r') && (buffer[6] == 'e') && (buffer[7] == 'e')) { siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3] - 8; fs.Seek(siz, SeekOrigin.Current); fs.Read(buffer, 0, 8); } if ((buffer[4] == 'm') && (buffer[5] == 'o') && (buffer[6] == 'o') && (buffer[7] == 'v')) { p.OptimizedForStreaming = "1"; m.OptimizedForStreaming = "1"; siz = (buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]) - 8; buffer = new byte[siz]; fs.Read(buffer, 0, siz); int opos; int oposmax; if (FindInBuffer("trak", 0, siz, buffer, out opos, out oposmax)) { if (FindInBuffer("mdia", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("minf", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("stbl", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("co64", opos, oposmax, buffer, out opos, out oposmax)) { p.Has64bitOffsets = "1"; } } } } } } } ex = 7; return(m); } catch (Exception e) { throw new Exception(ex + ":" + e.Message, e); } finally { mi.Close(); GC.Collect(); } }
public string GetSHA256(string path) { return(GetSHA256(File.OpenRead(path))); }
public string GetMD5(string path) { return(GetMD5(File.OpenRead(path))); }
public void ResetToStart(Action <Stream> AfterInit) { try { // in case the stream is at the beginning do nothing if (Stream != null && Stream.CanSeek) { Stream.Position = 0; } else { if (Stream != null) { Stream.Close(); // need to reopen the base stream BaseStream = File.OpenRead(BasePath); } if (AssumeGZip) { Log.Debug("Decompressing GZip Stream"); Stream = new System.IO.Compression.GZipStream(BaseStream, System.IO.Compression.CompressionMode.Decompress); } else if (AssumePGP) { System.Security.SecureString DecryptedPassphrase = null; // need to use the setting function, opening a form to enter the passphrase is not in this library if (string.IsNullOrEmpty(EncryptedPassphrase)) { throw new ApplicationException("Please provide a passphrase."); } try { DecryptedPassphrase = EncryptedPassphrase.Decrypt().ToSecureString(); } catch (Exception) { throw new ApplicationException("Please reenter the passphrase, the passphrase could not be decrypted."); } try { Log.Debug("Decrypt PGP Stream"); Stream = ApplicationSetting.ToolSetting.PGPInformation.PgpDecrypt(BaseStream, DecryptedPassphrase); } catch (Org.BouncyCastle.Bcpg.OpenPgp.PgpException ex) { // removed possibly stored passphrase var recipinet = string.Empty; try { recipinet = ApplicationSetting.ToolSetting?.PGPInformation?.GetEncryptedKeyID(BaseStream); } catch { // ignore } if (recipinet.Length > 0) { throw new ApplicationException($"The message is encrypted for '{recipinet}'.", ex); } else { throw; } } } else { Stream = BaseStream; } } } finally { AfterInit?.Invoke(Stream); } }
public static FileStream OpenRead(string path) { return(File.OpenRead(path)); }
static void Main(string[] args) { int totalProcessed = 0; int totalHashed = 0; int totalFailed = 0; IEnumerable <FilesDueForHash_Result> filesToHash = GetNextFiles(); do { Parallel.ForEach(filesToHash, fileToHash => { Console.WriteLine("Processing: " + fileToHash.FullPath); try { if (File.Exists(fileToHash.FullPath) && fileToHash.Length > 0) { Stream fileStream = File.OpenRead(fileToHash.FullPath); IHashMaker hasher = new HashMaker(); string preHash = hasher.GetPreHash(fileToHash.FullPath); string md5 = hasher.GetMD5(fileStream); string sha1 = hasher.GetSHA1(fileStream); using (DbModelContainer db = new DbModelContainer()) { db.Database.CommandTimeout = 60; TrackedFile fileToUpdate = db.TrackedFiles.Find(fileToHash.Id); fileToUpdate.PreHash = preHash; fileToUpdate.MD5 = md5; fileToUpdate.SHA1 = sha1; db.SaveChanges(); } Interlocked.Increment(ref totalHashed); } } catch (Exception) { //mark it as failed Interlocked.Increment(ref totalFailed); } finally { Interlocked.Increment(ref totalProcessed); using (DbModelContainer db = new DbModelContainer()) { db.Database.CommandTimeout = 60; TrackedFile file = db.TrackedFiles.Find(fileToHash.Id); file.HashAttempted = DateTime.Now; db.SaveChanges(); } } }); filesToHash = GetNextFiles(); } while (filesToHash.Count() > 0); }
private void CloseWrite() { // If we are writing we have possibly two steps, // a) compress / encrypt the data from temp // b) upload the data to sFTP if (ProcessDisplay == null) { ProcessDisplay = new DummyProcessDisplay(); } if (WritePath.AssumeGZip() || WritePath.AssumePgp()) { try { // Compress the file if (WritePath.AssumeGZip()) { Log.Debug("Compressing temporary file to GZip file"); using (var inFile = File.OpenRead(TempFile)) { // Create the compressed file. using (var outFile = File.Create(WritePath)) { using (var compress = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress)) { var processDispayTime = ProcessDisplay as IProcessDisplayTime; var inputBuffer = new byte[16384]; var max = (int)(inFile.Length / inputBuffer.Length); ProcessDisplay.Maximum = max; var count = 0; int length; while ((length = inFile.Read(inputBuffer, 0, inputBuffer.Length)) > 0) { compress.Write(inputBuffer, 0, length); ProcessDisplay.CancellationToken.ThrowIfCancellationRequested(); if (processDispayTime != null) { ProcessDisplay.SetProcess( $"GZip {processDispayTime.TimeToCompletion.PercentDisplay}{processDispayTime.TimeToCompletion.EstimatedTimeRemainingDisplaySeperator}", count); } else { ProcessDisplay.SetProcess($"GZip {count:N0}/{max:N0}", count); } count++; } } } } } // need to encrypt the file else if (WritePath.AssumePgp()) { using (FileStream inputStream = new FileInfo(TempFile).OpenRead(), output = new FileStream(WritePath.LongPathPrefix(), FileMode.Create)) { Log.Debug("Encrypting temporary file to PGP file"); ApplicationSetting.ToolSetting.PGPInformation.PgpEncrypt(inputStream, output, Recipient, ProcessDisplay); } } } finally { Log.Debug("Removing temporary file"); File.Delete(TempFile); } } }