/// <summary> /// writes a message using specific loglevel /// </summary> /// <param name="loglevel">a specific loglevel</param> /// <param name="message">text of the debug entry</param> private static void Write(LogLevel loglevel, string message) { PluploadConfiguration config = PluploadContext.GetConfiguration(); string logPath = config.GetPhysicalLogPath(); lock (lockThis) { if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } string logFile = Path.Combine(logPath, "plupload.log"); if (!File.Exists(logFile)) { using (FileStream fs = File.Create(logFile)) { } } using (StreamWriter sw = File.AppendText(logFile)) { sw.AutoFlush = true; sw.WriteLine(String.Format("{0} | {1} | {2} #end#", DateTime.Now, loglevel, message)); } } }
public void Should_Be_Disposed_When_Request_Pipeline_Completed() { var httpContextMock = new Mock<HttpContextBase>(); var httpContext = httpContextMock.Object; var pluploadContext = new PluploadContext(httpContext); httpContextMock.Verify(c => c.DisposeOnPipelineCompleted(pluploadContext), Times.Once); }
public void DeleteFiles_Should_Only_Dispose_And_Delete_Files_Of_Given_Reference() { string uploadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString()); var httpServerUtilityMock = new Mock<HttpServerUtilityBase>(); httpServerUtilityMock.Setup(u => u.MapPath(PluploadConfiguration.UploadPath)) .Returns(uploadPath); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.SetupGet(c => c.Server).Returns(httpServerUtilityMock.Object); var httpPostedFile1Mock = new Mock<HttpPostedFileBase>(); httpPostedFile1Mock.SetupGet(f => f.FileName).Returns("FileName1.Extension"); httpPostedFile1Mock.SetupGet(f => f.ContentLength).Returns(2); httpPostedFile1Mock.SetupGet(f => f.ContentType).Returns("application/pdf"); var httpPostedFile2Mock = new Mock<HttpPostedFileBase>(); httpPostedFile2Mock.SetupGet(f => f.FileName).Returns("FileName2.Extension"); httpPostedFile2Mock.SetupGet(f => f.ContentLength).Returns(2); httpPostedFile2Mock.SetupGet(f => f.ContentType).Returns("application/pdf"); using (var stream1 = new MemoryStream(new byte[] { 111, 222 })) using (var stream2 = new MemoryStream(new byte[] { 111, 222 })) { httpPostedFile1Mock.SetupGet(f => f.InputStream).Returns(stream1); httpPostedFile2Mock.SetupGet(f => f.InputStream).Returns(stream2); var reference1 = Guid.NewGuid().ToString(); var reference2 = "98w3jf3498sj"; using (var pluploadContext = new PluploadContext(httpContextMock.Object)) { pluploadContext.SaveFile(httpPostedFile1Mock.Object, reference1); pluploadContext.SaveFile(httpPostedFile2Mock.Object, reference2); var file2 = pluploadContext.GetFiles(reference2).FirstOrDefault(); pluploadContext.DeleteFiles(reference1); Assert.AreEqual(0, pluploadContext.GetFiles(reference1).Count()); Assert.IsTrue(!Directory.Exists(pluploadContext.GetUploadPath(reference1)) || !Directory.GetFiles(pluploadContext.GetUploadPath(reference1)).Any()); Assert.AreEqual(1, pluploadContext.GetFiles(reference2).Count()); Assert.AreEqual(2, Directory.GetFiles(pluploadContext.GetUploadPath(reference2)).Count()); Assert.IsTrue(file2.InputStream.Length > 0); } } try { Directory.Delete(uploadPath, true); } catch (IOException) { // Files could always be in use by virusscanners and what not.. So ignore it. } }
public void Should_Get_Saved_Chunk_Including_Content_Type() { string uploadPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString()); var httpServerUtilityMock = new Mock<HttpServerUtilityBase>(); httpServerUtilityMock.Setup(u => u.MapPath(PluploadConfiguration.UploadPath)) .Returns(uploadPath); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.SetupGet(c => c.Server).Returns(httpServerUtilityMock.Object); var httpPostedFileMock = new Mock<HttpPostedFileBase>(); httpPostedFileMock.SetupGet(f => f.FileName).Returns("document.pdf"); httpPostedFileMock.SetupGet(f => f.ContentLength).Returns(2); using (var stream = new MemoryStream(new byte[] { 111, 222 })) { httpPostedFileMock.SetupGet(f => f.InputStream).Returns(stream); var reference = Guid.NewGuid().ToString(); using (var pluploadContext = new PluploadContext(httpContextMock.Object)) { // Finish same chunked upload twice to test that file will be overwritten and does not throw exception. pluploadContext.SaveChunk(httpPostedFileMock.Object, reference, httpPostedFileMock.Object.FileName, 0, 1); pluploadContext.SaveChunk(httpPostedFileMock.Object, reference, httpPostedFileMock.Object.FileName, 0, 1); var file = pluploadContext.GetFiles(reference).FirstOrDefault(); Assert.IsNotNull(file); Assert.AreEqual(file.FileName, httpPostedFileMock.Object.FileName); Assert.AreEqual(file.ContentLength, httpPostedFileMock.Object.ContentLength); Assert.AreEqual(file.ContentType, "application/pdf"); Assert.AreEqual(file.InputStream.Length, httpPostedFileMock.Object.InputStream.Length); // TODO: Split up this test in separate tests for saving, getting and deleting. pluploadContext.DeleteFiles(reference); Assert.AreEqual(0, pluploadContext.GetFiles(reference).Count()); Assert.AreEqual(0, Directory.GetFiles(uploadPath).Count()); } } try { Directory.Delete(uploadPath, true); } catch (IOException) { // Files could always be in use by virusscanners and what not.. So ignore it. } }
public void Should_Throw_ArgumentException_When_Reference_Contains_Invalid_FileName_Chars() { var httpContextMock = new Mock<HttpContextBase>(); var httpContext = httpContextMock.Object; var pluploadContext = new PluploadContext(httpContext); try { pluploadContext.DeleteFiles("dsf:sdf"); } catch (ArgumentException ex) { Assert.AreEqual("reference", ex.ParamName); throw; } }