Exemplo n.º 1
0
        public async Task <IActionResult> ModifyFilenameAsync(string id)
        {
            string newFileName    = "";
            string newDirFullPath = "";
            string dirFullpath    = DirectoryHelper.GetCodeDirectoryPath();
            string reqBody        = "";
            await System.Threading.Tasks.Task.FromResult(0);

            //
            try
            {
                //read request body
                using (var reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();
                    reqBody = body.ToString();
                }
                //get new filename
                if (!string.IsNullOrEmpty(reqBody))
                {
                    var content = JObject.Parse(reqBody);
                    newFileName = (string)content["newName"];
                    newFileName = Regex.Replace(newFileName, "[\n\r\t]", string.Empty);
                    newFileName = Regex.Replace(newFileName, @"\s", string.Empty);
                }
                //if same name exist - BadRequest
                foreach (var record in codeResponse)
                {
                    if (record.Id.ToLower() == newFileName.ToLower())
                    {
                        return(BadRequest(new { message = "File with same name already exists." }));
                    }
                }

                if (!string.IsNullOrEmpty(newFileName))
                {
                    //rename the file and/or folder
                    foreach (var record in codeResponse)
                    {
                        if (record.Id.ToString() == id)
                        {
                            if (record.Type == "JUPYTER_NOTEBOOK")
                            {
                                //check if folder path exists...if not then move folder
                                newDirFullPath = $"{dirFullpath}{newFileName}";
                                if (!Directory.Exists(newDirFullPath))
                                {
                                    Directory.Move($"{dirFullpath}{id}", newDirFullPath);
                                    FileFolderHelper.RenameFile($"{newDirFullPath}/{id}.{record.Extension}", $"{newDirFullPath}/{newFileName}.{record.Extension}");
                                    newDirFullPath = $"{newDirFullPath}/{newFileName}.{record.Extension}";
                                }
                                else
                                {
                                    return(BadRequest(new { message = "Folder with same name already exists. Please choose different file/folder name." }));
                                }
                            }
                            else
                            {
                                newDirFullPath = record.FilePath.Replace($"{id}.{record.Extension}", $"{newFileName}.{record.Extension}");
                                FileFolderHelper.RenameFile(record.FilePath, newDirFullPath);
                            }
                            //update GlobalStorage dictionary
                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = newFileName,
                                Name        = $"{newFileName}.{record.Extension}",
                                User        = "",
                                Created_on  = record.Created_on,
                                Edited_on   = record.Edited_on,
                                Extension   = record.Extension,
                                MimeType    = record.MimeType,
                                Size        = record.Size,
                                Type        = record.Type,
                                Url         = record.Url.Replace(id, newFileName),
                                FilePath    = newDirFullPath,
                                DateCreated = record.DateCreated
                            };
                            CodePayload.Create(newRecord);
                            CodePayload.RemoveOnlyFromCodePayload(id);
                            return(Json(newRecord));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "Renaming file failed.", exception = ex.StackTrace }));
            }
            return(BadRequest(new { message = "Renaming file failed." }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ModifyFilenameAsync(string id)
        {
            string newFileName = "";
            string reqBody     = "";
            await System.Threading.Tasks.Task.FromResult(0);

            try
            {
                //read request body
                using (var reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();
                    reqBody = body.ToString();
                }
                //get new filename
                if (!string.IsNullOrEmpty(reqBody))
                {
                    var content = JObject.Parse(reqBody);
                    newFileName = (string)content["newName"];
                    newFileName = Regex.Replace(newFileName, "[\n\r\t]", string.Empty);
                    newFileName = Regex.Replace(newFileName, @"\s", string.Empty);
                }

                if (!string.IsNullOrEmpty(newFileName))
                {
                    //if same name exist - BadRequest
                    foreach (var record in responseData)
                    {
                        if (record.Id.ToLower() == newFileName.ToLower())
                        {
                            return(BadRequest(new { message = "File with same name already exists." }));
                        }
                    }
                    //rename the file and/or folder
                    foreach (var record in responseData)
                    {
                        if (record.Id.ToString() == id)
                        {
                            var newfilePath = record.FilePath.Replace($"{id}.{record.Extension}", $"{newFileName}.{record.Extension}");
                            FileFolderHelper.RenameFile(record.FilePath, newfilePath);
                            var newRecord = new ModelResponse()
                            {
                                Created_on = record.Created_on,
                                Edited_on  = record.Edited_on,
                                Extension  = record.Extension,
                                FilePath   = newfilePath,
                                Id         = newFileName,
                                MimeType   = record.MimeType,
                                Name       = $"{newFileName}.{record.Extension}",
                                Properties = record.Properties,
                                Size       = record.Size,
                                Type       = record.Type,
                                Url        = record.Url.Replace(id, newFileName),
                                User       = record.User
                            };
                            ModelPayload.Create(newRecord);
                            ModelPayload.RemoveOnlyFromModelPayload(id);
                            return(Json(newRecord));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "Renaming file failed.", exception = ex.StackTrace }));
            }

            return(BadRequest(new { message = "Renaming file failed." }));
        }