예제 #1
0
        protected override void ProcessRecord()
        {
            if (NewFolder != null)
            {
                NewFolderId = NewFolder.Id;
            }
            var newFolderPath = FolderHelper.GetFolderPath(NewFolderId, out bool isFolderSealed);

            if (newFolderPath == null)
            {
                throw new ArgumentException("Folder cannot be found.", nameof(newFolderPath));
            }

            if (File != null)
            {
                Id = File.Id;
            }

            if (!OverrideSealedFolder.IsPresent && isFolderSealed)
            {
                ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("Target folder is set to sealed and -" + nameof(OverrideSealedFolder) + " is not present."), "ImageStore Move File", ErrorCategory.SecurityError, NewFolderId));
            }



            var connection = DatabaseConnection.Current;

            var fileName = FileHelper.GetFileName(Id, out _, out string oldPath, out var fileNameWithoutPath, out isFolderSealed, out _);

            if (!SkipFile.IsPresent)
            {
                if (NewPath == null)
                {
                    NewPath = oldPath;
                }
                if (fileName == null)
                {
                    ThrowTerminatingError(new ErrorRecord(new ArgumentException("File cannot be found.", nameof(Id)), "ImageStore Move File", ErrorCategory.InvalidArgument, null));
                }
                if (!OverrideSealedFolder.IsPresent && isFolderSealed)
                {
                    ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("Folder containing this file is set to sealed and -" + nameof(OverrideSealedFolder) + " is not present."), "ImageStore Move File", ErrorCategory.SecurityError, File.FolderId));
                }
                if (System.IO.File.Exists(fileName))
                {
                    string newName;
                    if (NewPath == "")
                    {
                        newName = newFolderPath + DirectorySeparatorString.Value + fileNameWithoutPath;
                    }
                    else
                    {
                        var newDirectory = newFolderPath + DirectorySeparatorString.Value + NewPath;
                        newName = newDirectory + DirectorySeparatorString.Value + fileNameWithoutPath;
                        System.IO.Directory.CreateDirectory(newDirectory);
                    }

                    System.IO.File.Move(fileName, newName);
                    WriteInformation("File is moved from " + fileName + " to " + newName + ".", new string[] { "MoveFile" });
                }
                else
                {
                    WriteWarning("File to be renaming cannot be found. Path: " + fileName);
                }
            }

            using (var command = new SqlCommand("Update [File] Set [FolderId]=@FolderId, [Path]=@Path where [Id]=@Id"))
            {
                command.Connection = connection;
                command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier)
                {
                    Value = Id
                });
                command.Parameters.Add(new SqlParameter("@FolderId", System.Data.SqlDbType.UniqueIdentifier)
                {
                    Value = NewFolderId
                });
                command.Parameters.Add(new SqlParameter("@Path", System.Data.SqlDbType.NVarChar, 256)
                {
                    Value = NewPath
                });

                if (command.ExecuteNonQuery() > 0)
                {
                    if (SkipReturn.IsPresent)
                    {
                        WriteObject(null);
                    }
                    else
                    {
                        var newFile = GetFileCmdlet.GetFile(Id);
                        WriteObject(newFile);
                    }
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new InvalidOperationException("Cannot move this file."),
                                              "ImageStore Move File", ErrorCategory.WriteError, null));
                }
            }
        }
예제 #2
0
        protected override void ProcessRecord()
        {
            if (NewFileName == null)
            {
                throw new ArgumentNullException(nameof(NewFileName));
            }
            string extensionName;

            if (NewExtension != null)
            {
                NewExtensionId = NewExtension.Id;
                extensionName  = NewExtension.Extension;
            }
            else
            {
                extensionName = ExtensionHelper.GetExtensionName(NewExtensionId, out _, out _);
                if (extensionName == null)
                {
                    throw new ArgumentException("Extension cannot be found.", nameof(NewExtensionId));
                }
            }

            if (File != null)
            {
                Id = File.Id;
            }

            var connection = DatabaseConnection.Current;

            if (!SkipFile.IsPresent)
            {
                var fileName = FileHelper.GetFileName(Id, out var folderPath, out var path, out _, out var isFolderSealed, out var folderId);
                if (fileName == null)
                {
                    ThrowTerminatingError(new ErrorRecord(new ArgumentException("File cannot be found.", nameof(Id)), "ImageStore Rename File", ErrorCategory.InvalidArgument, null));
                }
                if (isFolderSealed && !OverrideSealedFolder.IsPresent)
                {
                    ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("Folder containing this file is set to sealed and -" + nameof(OverrideSealedFolder) + " is not present."), "ImageStore Rename File", ErrorCategory.SecurityError, folderId));
                }
                if (System.IO.File.Exists(fileName))
                {
                    string newName;
                    if (path != "")
                    {
                        newName = folderPath + path + DirectorySeparatorString.Value + NewFileName + "." + extensionName;
                    }
                    else
                    {
                        newName = folderPath + NewFileName + "." + extensionName;
                    }
                    System.IO.File.Move(fileName, newName);
                    WriteInformation("File is renamed from " + fileName + " to " + newName + ".", new string[] { "RenameFile" });
                }
                else
                {
                    WriteWarning("File to be renaming cannot be found. Path: " + fileName);
                }
            }

            using (var command = new SqlCommand("Update [File] Set [FileName]=@FileName, [ExtensionId]=@ExtensionId where [Id]=@Id"))
            {
                command.Connection = connection;
                command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier)
                {
                    Value = Id
                });
                command.Parameters.Add(new SqlParameter("@FileName", System.Data.SqlDbType.NVarChar, 256)
                {
                    Value = NewFileName
                });
                command.Parameters.Add(new SqlParameter("@ExtensionId", System.Data.SqlDbType.UniqueIdentifier)
                {
                    Value = NewExtensionId
                });

                if (command.ExecuteNonQuery() > 0)
                {
                    if (SkipReturn.IsPresent)
                    {
                        WriteObject(null);
                    }
                    else
                    {
                        var newFile = GetFileCmdlet.GetFile(File.Id);
                        WriteObject(newFile);
                    }
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new InvalidOperationException("Cannot rename this file."),
                                              "ImageStore Rename File", ErrorCategory.WriteError, null));
                }
            }
        }