Esempio n. 1
0
        public ActionResult _Upload()
        {
            var entityId    = Convert.ToInt32(Request.Params["EntityId"]);
            var fileType    = Request.Params["FileType"].ToEnum <EditionFileType>();
            var langCode    = Request.Params["LanguageCode"];
            var isSaved     = true;
            var newFileName = "";

            var edition = EditionServices.GetEditionById(entityId);

            if (edition == null)
            {
                return(View("NotFound", new ErrorModel {
                    Message = Constants.EditionNotFound
                }));
            }
            if (edition.IsCancelled())
            {
                return(Json(new { success = false, message = Constants.WarningMessageEventCancelled }));
            }

            try
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileNameWithoutExtension(file.FileName);

                    // CHECK EXTENSION
                    var extension         = Path.GetExtension(file.FileName);
                    var allowedExtensions = fileType.GetAttribute <FileTypeAttribute>().AllowedExtensions;

                    if (!allowedExtensions.Contains(extension))
                    {
                        return(Json(new { success = false, message = Constants.InvalidFileExtension }));
                    }

                    newFileName = EditionServiceHelper.ComposeFileName(edition.EditionId, fileName, fileType, langCode, extension);

                    // The way of saving changes depending on fileType's Private property.
                    var result = fileType.GetAttribute <FileTypeAttribute>().Private
                        ? SaveFileToLocalDisk(fileType, newFileName, file)
                        : SaveFileToAzureStorage(fileType, newFileName, file);

                    switch (result.Result)
                    {
                    case OperationResult.AlreadyExists:
                        return(Json(new { success = false, message = Constants.FileAlreadyExistsWithTheSameName }));

                    case OperationResult.Failed:
                        return(Json(new { success = false, message = result.Message }));
                    }

                    // CREATE FILE
                    if (!SaveFileInfo(entityId, newFileName, fileType, extension, langCode))
                    {
                        return(Json(new { success = false, message = Constants.ErrorWhileSavingFile }));
                    }
                }
            }
            catch (Exception exc)
            {
                isSaved = false;

                var log = CreateInternalLog(exc);
                ExternalLogHelper.Log(log, LoggingEventType.Error);
            }

            if (!isSaved)
            {
                return(Json(new { success = false, message = Constants.ErrorWhileSavingFile }));
            }

            // UPDATE EDITION
            UpdateEditionUpdateInfo(edition);

            // UPDATE LOG
            var updatedFields = NotificationControllerHelper.GetUpdatedFieldsAsJson($"File ({fileType})", new List <Variance> {
                new Variance {
                    Prop = fileType.ToString()
                }
            });

            UpdateLogInMemory(edition, updatedFields);

            //UpdateLogInMemory(currentEdition, edition, currentEditionTranslation, editionTranslation);

            // UPDATEDCONTENT
            var currentFileForComparison = new FileEntity();
            var fileForComparison        = new FileEntity {
                FileName = newFileName
            };

            fileForComparison.SetFullUrl();
            var updatedContent = NotificationControllerHelper.GetUpdatedContent(currentFileForComparison, fileForComparison);

            PushEditionUpdateNotifications(edition, updatedContent);

            return(Json(new { success = true, message = Constants.FileSaved }));
        }
Esempio n. 2
0
        public ActionResult _Delete(int fileId, string fileType)
        {
            var editionFileType = fileType.ToLower().ToEnumFromDescription <EditionFileType>();

            var file = FileServices.GetFileById(fileId);

            if (file == null)
            {
                return(Json(new { success = false, message = Constants.FileNotFound }));
            }

            var edition = EditionServices.GetEditionById(file.EntityId);

            if (edition.IsCancelled())
            {
                return(Json(new { success = false, message = Constants.WarningMessageEventCancelled }));
            }

            var success = FileServices.DeleteFile(file.FileId);

            if (success)
            {
                bool success2;
                if (file.EditionFileType.GetAttribute <FileTypeAttribute>().Private)
                {
                    success2 = DeleteFileFromLocalDisk(file.FileName, file.EditionFileType);
                }
                else
                {
                    var blobName = editionFileType.BlobFullName(file.FileName);

                    try
                    {
                        success2 = _azureStorageService.DeleteFile(blobName);
                    }
                    catch (Exception exc)
                    {
                        var log = CreateInternalLog(exc);
                        ExternalLogHelper.Log(log, LoggingEventType.Error);

                        success2 = false;
                    }
                }

                //if (!success2)
                //    return Json(new {success = false, message = "File could not be deleted!"});

                // UPDATE EDITON
                UpdateEditionUpdateInfo(edition);

                //// DIFF
                //var diff = new List<Variance> { new Variance { Prop = $"File ({file.EditionFileType})", ValA = file.FileName, ValB = null } };

                //OnEditionUpdated(edition, diff);

                // UPDATE LOG
                var updatedFields = NotificationControllerHelper.GetUpdatedFieldsAsJson($"File ({file.EditionFileType})", new List <Variance> {
                    new Variance {
                        Prop = editionFileType.ToString()
                    }
                });
                UpdateLogInMemory(edition, updatedFields);

                // UPDATEDCONTENT
                var currentFileForComparison = new FileEntity {
                    FileName = file.FileName
                };
                var fileForComparison = new FileEntity();
                fileForComparison.SetFullUrl();
                var updatedContent = NotificationControllerHelper.GetUpdatedContent(currentFileForComparison, fileForComparison);

                PushEditionUpdateNotifications(edition, updatedContent);

                return(Json(new { success = true, message = Constants.FileDeleted, fileType = editionFileType.GetDescription() }));
            }
            return(Json(new { success = false, message = Constants.FileNotDeleted }));
        }