Esempio n. 1
0
        public async Task <object> Get(GetFileUrl request)
        {
            ServiceModel.Types.File file = await GetFileByIdOrThrowAsync(request.FileId);

            var redirectUrl = $"{Request.GetBaseUrl()}/files/{file.FileName}";

            return(new GetFileUrlResponse
            {
                Url = redirectUrl
            });
        }
Esempio n. 2
0
        public async Task <object> PostAsync(FileUpload request)
        {
            var feature = HostContext.GetPlugin <FileFeature>();

            List <ServiceModel.Types.File> newFiles = new List <ServiceModel.Types.File>();

            using (var trans = Db.OpenTransaction())
            {
                try
                {
                    foreach (IHttpFile requestFile in Request.Files)
                    {
                        var fileNameFromReq = requestFile.FileName;
                        var fileFullPath    =
                            feature.UploadedFileFullPathBuilder?.Invoke(request) ?? ServiceConfig.Instance.GetDefaultUploadFullFilePath(fileNameFromReq, request.Folder);

                        VirtualFiles.WriteFile(fileFullPath, requestFile.InputStream);

                        var fileRecord = new ServiceModel.Types.File()
                        {
                            ApplicationId   = request.ApplicationId,
                            ContentType     = requestFile.ContentType,
                            ExtraAttribute1 = request.ExtraAttribute1,
                            ExtraAttribute2 = request.ExtraAttribute2,
                            ExtraAttribute3 = request.ExtraAttribute3,
                            ExtraAttribute4 = request.ExtraAttribute4,
                            FileName        = requestFile.FileName,
                            ReferencedBy    = request.ReferencedBy,
                            FileHash        = requestFile.InputStream.ComputeFileMd5(),
                            CreatedBy       = GetSession().GetUserAuthName(),
                            CreatedDate     = DateTime.UtcNow,
                            ModifiedBy      = GetSession().GetUserAuthName(),
                            ModifiedDate    = DateTime.UtcNow
                        };
                        await Db.InsertAsync(fileRecord);

                        newFiles.Add(fileRecord);
                    }

                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new HttpError(HttpStatusCode.InternalServerError, e);
                }
            }

            return(new FileUploadResponse()
            {
                Ids = newFiles.Select(f => f.Id).ToList(), Results = newFiles
            });
        }
Esempio n. 3
0
        public async Task <object> Get(GetFileContent request)
        {
            ServiceModel.Types.File fileDb = await GetFileByIdOrThrowAsync(request.Id);

            var root     = ServiceConfig.Instance.DefaultUploadsPath.Replace("~", "").MapServerPath();
            var filePath = $"{root}/{fileDb.FileName}";
            var file     = VirtualFiles.GetFile(filePath);
            var content  = file.ReadAllBytes();

            return(new GetFileContentResponse()
            {
                Content = content,
                ContentType = fileDb.ContentType
            });
        }