예제 #1
0
        public async Task <string> Update(Guid binaryObjectId, IFormFile file, string organizationId = "", string apiComponent = "", string name = "")
        {
            //Update file in OpenBots.Server.Web using relative directory
            binaryObjectManager.Update(file, organizationId, apiComponent, binaryObjectId);

            //find relative directory where binary object is being saved
            string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObjectId.ToString());

            await binaryObjectManager.UpdateEntity(file, filePath, binaryObjectId.ToString(), apiComponent, apiComponent, name);

            return("Success");
        }
        public async Task <IActionResult> Update(string id, [FromForm] BinaryObjectViewModel request)
        {
            try
            {
                Guid entityId = new Guid(id);

                if (string.IsNullOrEmpty(request.Folder))
                {
                    request.Folder = request.CorrelationEntity;
                }

                var existingbinary = repository.Find(null, x => x.Folder?.ToLower(null) == request.Folder?.ToLower(null) && x.Name.ToLower(null) == request.File?.FileName?.ToLower(null) && x.Id != entityId)?.Items?.FirstOrDefault();
                if (existingbinary != null)
                {
                    ModelState.AddModelError("BinaryObject", "Same file name already exists in the given folder");
                    return(BadRequest(ModelState));
                }

                var existingBinaryObject = repository.GetOne(entityId);
                if (existingBinaryObject == null)
                {
                    return(NotFound());
                }

                string organizationId = existingBinaryObject.OrganizationId.ToString();
                if (!string.IsNullOrEmpty(organizationId))
                {
                    organizationId = binaryObjectManager.GetOrganizationId().ToString();
                }

                string apiComponent = existingBinaryObject.CorrelationEntity;

                if (request == null)
                {
                    ModelState.AddModelError("Save", "No data passed");
                    return(BadRequest(ModelState));
                }

                long size = request.File.Length;

                if (existingBinaryObject.Id != Guid.Empty && size > 0)
                {
                    existingBinaryObject.Name        = request.Name;
                    existingBinaryObject.ContentType = request.File.ContentType;
                    existingBinaryObject.SizeInBytes = request.File.Length;
                    existingBinaryObject.Folder      = request.Folder;

                    //Update file in OpenBots.Server.Web using relative directory
                    binaryObjectManager.Update(request.File, organizationId, apiComponent, Guid.Parse(id));
                    await binaryObjectManager.UpdateEntity(request.File, existingBinaryObject.StoragePath, existingBinaryObject.Id.ToString(), apiComponent, existingBinaryObject.Folder, existingBinaryObject.Name);
                }
                else
                {
                    existingBinaryObject.Name   = request.Name;
                    existingBinaryObject.Folder = request.Folder;
                    await base.PutEntity(id, existingBinaryObject);
                }

                return(Ok(existingBinaryObject));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }