public void WriteCodeGeneratorResponse(CodeGeneratorResponse response) { try { if (null == response.CodeGeneratorContext || response.Errors.Any()) { return; } var codeBehindFileName = _codeBehindFileHelper.GetOrAddCodeBehindFile( response.CodeGeneratorContext.Source.FileName); if (codeBehindFileName.IsNullOrEmpty()) { _log.Error("Code Behind File Helper returned a null or empty CodeBehindFileName. Can not write file."); return; } _log.InfoFormat("Updating [{0}]", codeBehindFileName); if (_fileWrapper.Exists(codeBehindFileName)) { _log.DebugFormat("Deleting file [{0}]", codeBehindFileName); _fileWrapper.Delete(codeBehindFileName); } var codeBehindFileSource = response.GeneratedCodeSyntaxTree.GetText(); if (string.IsNullOrEmpty(codeBehindFileSource)) { _log.WarnFormat("Writing Empty Code Behind File for [{0}]", codeBehindFileName); } _fileWrapper.WriteAllText(codeBehindFileName, codeBehindFileSource); _fileReader.EvictFromCache(codeBehindFileName); var openWindow = _visualStudioOpenDocumentManager.GetOpenDocument(codeBehindFileName); if (null != openWindow) { openWindow.WriteText(codeBehindFileSource); } } catch (Exception e) { _log.Error( string.Format("Exception writing Generated Code for Source Class [{0}]: {1}", response.CodeGeneratorContext.Source.FileName, e.Message), e); } }
public void Delete(string filePath) { if (_fileWrapper.Exists(filePath)) { _fileWrapper.Delete(filePath); } }
public virtual void DeleteFile() { if (!_fileWrapper.Exists(FilePath)) { return; } _fileWrapper.Delete(FilePath); }
public void DeleteFile_FileExistAndGotDeleted_ReturnFileStatusDeleteSuccess() { A.CallTo(() => _fileWrapper.Exists(A <string> ._)).ReturnsNextFromSequence(true, false); //Act var result = sut.DeleteFile("TestFile.txt", new List <string> { "UnitTestFolder" }); //Assert result.Should().NotBeNull(); result.Should().BeOfType <FileStatus>(); result.Should().Be(FileStatus.DeleteSuccess); A.CallTo(() => _pathWrapper.Combine(A <string[]> ._)).MustHaveHappened(1, FakeItEasy.Times.Exactly); A.CallTo(() => _fileWrapper.Exists(A <string> ._)).MustHaveHappened(1, FakeItEasy.Times.Exactly); A.CallTo(() => _fileWrapper.Delete(A <string> ._)).MustHaveHappened(1, FakeItEasy.Times.Exactly); }
public void UpdateCardsDB(string newContent) { string fileName = ".\\Resources\\cards.json"; IFileWrapper fileWrapper = trackerfactory.GetService <IFileWrapper>(); ICardsDatabase cardsDatabase = trackerfactory.GetService <ICardsDatabase>(); string backupFileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(fileName), cardsDatabase.Version, Path.GetExtension(fileName)); //includes . backupFileName = Path.Combine(Path.GetDirectoryName(fileName), backupFileName); if (fileWrapper.Exists(backupFileName)) { fileWrapper.Delete(backupFileName); } fileWrapper.Move(fileName, backupFileName); fileWrapper.WriteAllText(fileName, newContent); trackerfactory.GetService <ICardsDatabase>().RealoadDB(); }
public void ManageBackups( string path, IPathWrapper pathWrapper, IDirectoryWrapper directoryWrapper, IFileWrapper fileWrapper) { string dataFileFilter = Path.ChangeExtension( string.Format("{0}*", Path.GetFileNameWithoutExtension(DataFile)), Path.GetExtension(DataFile)); var backupFiles = directoryWrapper.EnumerateFiles( pathWrapper.GetDirectoryName(path), dataFileFilter).Where(f => f != FullDataFilePath).OrderByDescending(f => f); //first save of day - delete old backups int backupcount = backupFiles.Count(); int skipfiles = 7; //backups to keep if (backupcount > skipfiles) { foreach (string s in backupFiles.Skip(skipfiles)) { fileWrapper.Delete(s); } } }
public async Task <HttpResponseMessage> Upload() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // Key - file name, Value - error of the loading var uploadFileInfos = new List <UploadResultViewModel>(); try { // Get temporary album Id int tempAlbumId = _albumService.GetAlbumId(User.Id, "Temporary"); // Get path to the temporary album folder string pathToTempAlbum = _pathUtil.BuildAbsoluteAlbumPath(User.Id, tempAlbumId); // Create directory, if it isn't exist if (!_directoryWrapper.Exists(pathToTempAlbum)) { _directoryWrapper.CreateDirectory(pathToTempAlbum); } // TODO create this instance with fabrik var provider = new MultipartFormDataStreamProvider(pathToTempAlbum); // Read the form data from request (save all files in selected folder) TODO must be wrapped too await Request.Content.ReadAsMultipartAsync(provider); int albumId = _albumService.GetAlbumId(User.Id, "Temporary"); // Check all uploaded files foreach (MultipartFileData fileData in provider.FileData) { string originalFileName = fileData.Headers.ContentDisposition.FileName.Trim('"'); long fileSize = _fileHelper.GetFileSize(fileData.LocalFileName); string fileHash = _cryptoProvider.GetHash(string.Format("{0}{1}", originalFileName, fileSize)); if (fileSize > MAX_PHOTO_SIZE_IN_BYTES) { _fileWrapper.Delete(fileData.LocalFileName); uploadFileInfos.Add(new UploadResultViewModel { Hash = fileHash, IsAccepted = false, Error = "This file contains no image data" }); } // Is it really image file format ? if (!_fileHelper.IsImageFile(fileData.LocalFileName)) { _fileWrapper.Delete(fileData.LocalFileName); uploadFileInfos.Add(new UploadResultViewModel { Hash = fileHash, IsAccepted = false, Error = "This file contains no image data" }); continue; } string format = _fileHelper.GetRealFileFormat(fileData.LocalFileName); var photoModel = _photoService.AddPhoto(PhotoViewModel.ToModel(User.Id, albumId, format)); string destFileName = string.Format("{0}\\{1}.{2}", pathToTempAlbum, photoModel.Id, format); try { if (File.Exists(destFileName)) { _fileWrapper.Delete(destFileName); } _fileWrapper.Move(fileData.LocalFileName, destFileName); } catch (IOException) { _fileWrapper.Delete(fileData.LocalFileName); uploadFileInfos.Add(new UploadResultViewModel { Hash = fileHash, IsAccepted = false, Error = "File already uploaded" }); continue; } // Create thumbnails for photo _photoProcessor.CreateThumbnails(User.Id, albumId, photoModel.Id, photoModel.Format); uploadFileInfos.Add(new UploadResultViewModel { Hash = fileHash, Id = photoModel.Id, IsAccepted = true }); } // Create collage for album 'Temporary' _collageProcessor.CreateCollage(User.Id, albumId); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } return(Request.CreateResponse(HttpStatusCode.OK, uploadFileInfos, new JsonMediaTypeFormatter())); }