Ejemplo n.º 1
0
        public IHttpActionResult Post()
        {
            try
            {
                List <FileViewModel> pushedfiles = new List <FileViewModel>();
                List <FileViewModel> failedfiles = new List <FileViewModel>();

                var userID = _genericHelper.GetUserID();

                if (userID != null)
                {
                    var files = _genericHelper.GetFilesFromHttpMessage();

                    if (files != null && files.Count > 0)
                    {
                        foreach (var fileContent in files)
                        {
                            var fileID = Guid.NewGuid();

                            if (fileContent != null && fileContent.ContentLength > 0)
                            {
                                var extension   = Path.GetExtension(fileContent.FileName);
                                var isFileValid = _genericHelper.IsFileAccepted(_applicationConfig, extension);

                                if (isFileValid == false)
                                {
                                    //do logic to report failed uploads //log for info purposes
                                    continue;
                                }

                                var stream = fileContent.InputStream;
                                var url    = _fileUploadHelper.UploadFile(_applicationConfig, stream, fileID.ToString() + extension);

                                if (url != null)
                                {
                                    FileMetaData fileMeta = new FileMetaData();
                                    fileMeta.UserID   = userID;
                                    fileMeta.ID       = fileID;
                                    fileMeta.BlobUrl  = url;
                                    fileMeta.FileSize = fileContent.ContentLength;
                                    fileMeta.Filename = fileContent.FileName;

                                    var socketID = _genericHelper.GetCurrentSocketID();

                                    QueueFileMetaDataModel queueItem = new QueueFileMetaDataModel(fileMeta, socketID);
                                    queueItem.MappingID = fileContent.FileKey;
                                    _messageQueueHelper.PushMessage <QueueFileMetaDataModel>(_applicationConfig, queueItem, _applicationConfig.FileDataCreateQueue);

                                    pushedfiles.Add(new FileViewModel(fileMeta.ID, fileMeta.UserID, fileMeta.Filename, fileMeta.FileExtension, fileMeta.BlobUrl, fileMeta.ViewCount, fileMeta.FileSize));
                                }
                            }
                        }
                    }

                    return(Ok(pushedfiles));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"File/Post failed", ex);
                return(InternalServerError());
            }

            return(BadRequest());
        }