예제 #1
0
        private void uploadCodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            UpdateTreeRefresher updateTreeRefresher = this.CreateUpdateTreeRefresher(this.EntityToken);

            FlowControllerServicesContainer flowControllerServicesContainer = WorkflowFacade.GetFlowControllerServicesContainer(WorkflowEnvironment.WorkflowInstanceId);
            var managementConsoleMessageService = flowControllerServicesContainer.GetService <IManagementConsoleMessageService>();

            UploadedFile uploadedFile = this.GetBinding <UploadedFile>("UploadedFile");
            IMediaFile   mediaFile    = this.GetBinding <IMediaFile>("File");

            if (uploadedFile.HasFile)
            {
                string mimeType = MimeTypeInfo.GetMimeType(uploadedFile);

                // Image cannot be replaced with a file of not image mime type
                string imageMimeTypePrefix = "image/";
                if ((mediaFile.MimeType.StartsWith(imageMimeTypePrefix, StringComparison.OrdinalIgnoreCase) &&
                     !mimeType.StartsWith(imageMimeTypePrefix, StringComparison.OrdinalIgnoreCase)))
                {
                    managementConsoleMessageService.CloseCurrentView();
                    string failure        = StringResourceSystemFacade.GetString("Composite.Management", "UploadNewMediaFileWorkflow.UploadFailure");
                    string failureMessage = StringResourceSystemFacade.GetString("Composite.Management", "UploadNewMediaFileWorkflow.UploadFailureMessage");
                    managementConsoleMessageService.ShowMessage(DialogType.Message, failure, failureMessage);
                    return;
                }

                using (System.IO.Stream readStream = uploadedFile.FileStream)
                {
                    using (System.IO.Stream writeStream = mediaFile.GetNewWriteStream())
                    {
                        readStream.CopyTo(writeStream);
                    }
                }
            }

            DataFacade.Update(mediaFile);

            SetSaveStatus(true);

            SelectElement(mediaFile.GetDataEntityToken());

            updateTreeRefresher.PostRefreshMesseges(mediaFile.GetDataEntityToken());
        }
예제 #2
0
        private void finalizeCodeActivity_Finalize_ExecuteCode(object sender, EventArgs e)
        {
            AddNewTreeRefresher addNewTreeRefresher = this.CreateAddNewTreeRefresher(this.EntityToken);

            UploadedFile uploadedFile = this.GetBinding <UploadedFile>("UploadedFile");
            string       filename;

            if (this.BindingExist("Filename"))
            {
                filename = this.GetBinding <string>("Filename");
            }
            else
            {
                filename = uploadedFile.FileName;
            }

            using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope())
            {
                IMediaFileStore store = DataFacade.GetData <IMediaFileStore>(x => x.Id == this.StoreId).First();

                IMediaFile existingFile = GetExistingFile(this.FolderPath, filename);

                if (existingFile == null)
                {
                    WorkflowMediaFile mediaFile = new WorkflowMediaFile();
                    mediaFile.FileName    = System.IO.Path.GetFileName(filename);
                    mediaFile.FolderPath  = this.FolderPath;
                    mediaFile.Title       = this.GetBinding <string>("Title");
                    mediaFile.Description = this.GetBinding <string>("Description");
                    mediaFile.Tags        = this.GetBinding <string>("Tags");
                    mediaFile.Culture     = C1Console.Users.UserSettings.ActiveLocaleCultureInfo.Name;
                    mediaFile.Length      = uploadedFile.ContentLength;
                    mediaFile.MimeType    = MimeTypeInfo.GetMimeType(uploadedFile);

                    using (System.IO.Stream readStream = uploadedFile.FileStream)
                    {
                        using (System.IO.Stream writeStream = mediaFile.GetNewWriteStream())
                        {
                            readStream.CopyTo(writeStream);
                        }
                    }

                    IMediaFile addedFile = DataFacade.AddNew <IMediaFile>(mediaFile, store.DataSourceId.ProviderName);

                    addNewTreeRefresher.PostRefreshMesseges(addedFile.GetDataEntityToken());

                    SelectElement(addedFile.GetDataEntityToken());
                }
                else
                {
                    Guid           fileId   = existingFile.Id;
                    IMediaFileData fileData = DataFacade.GetData <IMediaFileData>(file => file.Id == fileId).FirstOrDefault();

                    fileData.Title       = this.GetBinding <string>("Title");
                    fileData.Description = this.GetBinding <string>("Description");
                    fileData.Tags        = this.GetBinding <string>("Tags");
                    fileData.MimeType    = MimeTypeInfo.GetMimeType(uploadedFile);
                    fileData.Length      = uploadedFile.ContentLength;

                    using (System.IO.Stream readStream = uploadedFile.FileStream)
                    {
                        using (System.IO.Stream writeStream = existingFile.GetNewWriteStream())
                        {
                            readStream.CopyTo(writeStream);
                        }
                    }

                    DataFacade.Update(existingFile);
                    DataFacade.Update(fileData);

                    addNewTreeRefresher.PostRefreshMesseges(existingFile.GetDataEntityToken());

                    SelectElement(existingFile.GetDataEntityToken());
                }

                transactionScope.Complete();
            }
        }