public Result CopyFile(File source, Folder target, string newFileName, OverwriteBehavior behavior, out File file) { file = null; if (source == null) throw new ArgumentNullException("source"); if (target == null) throw new ArgumentNullException("target"); if (string.IsNullOrWhiteSpace(newFileName)) throw new ArgumentNullException("newFileName"); if (source.IsDeleted || target.IsDeleted) return Result.CannotModifyDeletedItems; //See if the file already exists file = GetFile(target, newFileName); if (file != null) { if (behavior == OverwriteBehavior.RaiseConflict) return Result.FileAlreadyExists; //else if (behavior == OverwriteBehavior.Overwrite) //{ // // ??? //} else if (behavior == OverwriteBehavior.Copy) { do { //Assume the extension is anything after the first ".". Put the " - Copy" before the file extension. int extensionDot = newFileName.IndexOf('.'); newFileName = newFileName.Substring(0, extensionDot) + " - Copy" + newFileName.Substring(extensionDot); file = GetFile(target, newFileName); } while (file != null); } } FileBase newFile = new FileBase() { Name = newFileName, ParentFolderId = target.Id, CreatedTimeStamp = DateTime.UtcNow, LastModifiedTimestamp = DateTime.UtcNow, IsDeleted = false, IsPurged = false }; db.Insert<FileBase>(newFile); long fileId = db.GetLastInsertId(); // Point to the tip version of the source file db.Insert<FileVersion>(new FileVersion() { FileId = fileId, Version = 1, Size = source.Size, FileStore = source.FileStore, IsCurrent = true, IsDeleted = false }); file = GetFile(fileId); return Result.Success; }
public Result CreateFile(Folder folder, string fileName, IO.Stream stream, out File file) { file = null; Guid fileStoreGuid; long fileSize; PersistStreamInternal(stream, out fileStoreGuid, out fileSize); File existingFile = GetFile(folder, fileName); long fileId; int nextVersion; if (existingFile != null) { fileId = existingFile.Id; nextVersion = GetNextVersion(existingFile); } else { nextVersion = 1; FileBase newFile = new FileBase() { Name = fileName, ParentFolderId = folder.Id, FullPath = folder.FullPath + fileName, CreatedTimeStamp = DateTime.UtcNow, LastModifiedTimestamp = DateTime.UtcNow, IsDeleted = false, IsPurged = false }; db.Insert<FileBase>(newFile); fileId = db.GetLastInsertId(); } db.Update<FileVersion>("IsCurrent = 0", "FileId = {0}".Params(fileId)); db.Insert<FileVersion>(new FileVersion() { FileId = fileId, Version = nextVersion, Size = fileSize, FileStore = fileStoreGuid, IsCurrent = true, IsDeleted = false }); file = GetFile(fileId); return Result.Success; }