示例#1
0
 private Task <string> ImportFileThread(ImporterState state)
 {
     return(Task.Run(() =>
     {
         try
         {
             state.Importer.Import(state.Stream, state.Destination, state.Options);
             return string.Empty;
         }
         catch (Exception ex)
         {
             _logger.Error("Can't import data because, ", ex);
             return ex.StackTrace;
         }
     }));
 }
示例#2
0
        public virtual async Task <IHttpActionResult> PostCmsImport(Guid id)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                _logger.Error(InvalidMediaTypeMessage, Request.CreateResponseException(InvalidMediaTypeMessage, HttpStatusCode.UnsupportedMediaType));
                throw Request.CreateResponseException(InvalidMediaTypeMessage, HttpStatusCode.UnsupportedMediaType);
            }

            var file = await Request.GetUploadedFile(UploadPaths.IntegrationDataPath);

            var destinationRoot = _permanentLinkMapper.Find(id);

            if (destinationRoot == null)
            {
                return(Ok(id));
            }

            var importerOptions = ImportOptions.DefaultOptions;

            importerOptions.KeepIdentity                = true;
            importerOptions.ValidateDestination         = true;
            importerOptions.EnsureContentNameUniqueness = true;
            importerOptions.IsTest = false;
            var importer = _dataImporterAccessor();

            PrincipalInfo.RecreatePrincipalForThreading();
            var state = new ImporterState
            {
                Destination = destinationRoot.ContentReference,
                Importer    = importer,
                Options     = importerOptions,
                Stream      = new FileStream(file.LocalFileName, FileMode.Open)
            };

            var message = await ImportFileThread(state);

            if (string.IsNullOrEmpty(message))
            {
                return(Ok(id));
            }
            else
            {
                throw Request.CreateResponseException(message, HttpStatusCode.InternalServerError);
            }
        }