Esempio n. 1
0
        /*
        **
        ** Upload
        **
        */

        public virtual HttpResponseMessage UploadFile(Client currentClient, Document entity, HttpRequestMessage Request, IHomeRepository homeRepo, IDocumentLogRepository logRepo)
        {
            MyMultipartFileStreamProvider provider;
            Boolean isImage = false;

            ValidateNull(entity);
            if (entity != null && ((IDocumentRepository)repo).GetDocumentById(entity.Id, currentClient.Id) == null)
            {
                validationDictionnary.AddModelError(TypeOfName.GetNameFromType <Document>(), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST);
            }
            if (!((DocumentValidation)validation).UploadValidationBeforeProvider(validationDictionnary, currentClient, entity, Request))
            {
                throw new ManahostValidationException(validationDictionnary);
            }
            provider = new MyMultipartFileStreamProvider(ManahostUploadFileSystem.GetUploadFolderPath(WebApiApplication.UPLOAD_FOLDER_ROOT, currentClient.Id, (Boolean)entity.IsPrivate));
            try
            {
                IEnumerable <HttpContent> parts = null;
                Task.Factory.StartNew(() => parts = Request.Content.ReadAsMultipartAsync(provider).Result.Contents,
                                      CancellationToken.None,
                                      TaskCreationOptions.LongRunning, // guarantees separate thread
                                      TaskScheduler.Default).Wait();
                MultipartFileData file = provider.FileData.First();

                if (!((DocumentValidation)validation).UploadValidationAfterProvider(validationDictionnary, currentClient, file))
                {
                    throw new ManahostValidationException(validationDictionnary);
                }
                if (isImage = ManahostUploadFileSystem.imageExtension.Any(s => entity.Title.EndsWith(s, StringComparison.OrdinalIgnoreCase)))
                {
                    ImageCompressAndThumbnail(file.LocalFileName);
                }
                if ((Boolean)entity.IsPrivate && currentClient != null)
                {
                    EncryptFileOnUpload(file.LocalFileName, DocumentUtils.GetEncryptionPassword(homeRepo, currentClient), isImage);
                }
                if (!UpdateEntitiesOnUpload(currentClient, entity, file.LocalFileName, isImage, logRepo))
                {
                    throw new ManahostValidationException(validationDictionnary);
                }
            }
            catch (Exception e)
            {
                if (validationDictionnary.IsValid)
                {
                    validationDictionnary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "Upload"), GenericError.INVALID_GIVEN_PARAMETER);
                }
                if (entity.Url != null)
                {
                    DocumentUtils.DeleteAllFile(entity.Url, isImage);
                }
                repo.Delete(entity);
                repo.Save();
                throw new ManahostValidationException(validationDictionnary, e.StackTrace);
            }
            return(BuildStringContent.BuildFromRequestOK(Request));
        }
Esempio n. 2
0
        /*
        **
        ** Download
        **
        */

        public virtual HttpResponseMessage Get(Document entity, HttpRequestMessage Request, object adds, String extensionName = null, Client currentClient = null, int?idHome = null)
        {
            if (!((DocumentValidation)validation).DownloadValidation(validationDictionnary, currentClient, entity))
            {
                throw new ManahostValidationException(validationDictionnary);
            }
            if (extensionName != null)
            {
                entity.Url   = DocumentUtils.GetNewPathFileName(DocumentUtils.GetFullDocumentUrl(entity.Url), extensionName, true);
                entity.Title = Path.GetFileNameWithoutExtension(entity.Title) + "_" + extensionName + Path.GetExtension(entity.Title);
            }
            try
            {
                HttpResponseMessage response = new HttpResponseMessage();
                Stream result = DocumentUtils.GetDocumentStream((Boolean)entity.IsPrivate, entity.Url, DocumentUtils.GetEncryptionPassword(HomeRepository, currentClient));

                entity.MimeType     = DocumentUtils.GetMimeType(entity.Title);
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = new StreamContent(result);
                response.Content.Headers.ContentType        = new MediaTypeHeaderValue(entity.MimeType);
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = entity.Title
                };
                return(response);
            }
            catch (IOException)
            {
                validationDictionnary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <Document>(), "Download"), GenericError.FILE_NOT_FOUND);
                throw new ManahostValidationException(validationDictionnary);
            }
        }