Example #1
0
        private UploadResult ConvertResult(UploadMetadataResponse metaData, UploadResult fileDataReq)
        {
            UploadResult               result   = new UploadResult(fileDataReq.Response);
            UploadMetadataResponse     fileData = JsonConvert.DeserializeObject <UploadMetadataResponse>(fileDataReq.Response);
            UploadMetadataResponseFile actFile  = metaData.files.First().Value;

            result.URL = $"{Settings.URL}/file/{metaData.id}/{actFile.id.ToString()}/{actFile.fileName}";
            return(result);
        }
Example #2
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (string.IsNullOrEmpty(Settings.URL))
            {
                throw new Exception("Plik Host is empty.");
            }
            NameValueCollection requestHeaders = new NameValueCollection();

            requestHeaders["X-PlikToken"] = Settings.APIKey;
            UploadMetadataRequest metaDataReq = new UploadMetadataRequest();

            metaDataReq.Files                = new UploadMetadataRequestFile0();
            metaDataReq.Files.File0          = new UploadMetadataRequestFile();
            metaDataReq.Files.File0.FileName = fileName;
            metaDataReq.Files.File0.FileType = RequestHelpers.GetMimeType(fileName);
            metaDataReq.Files.File0.FileSize = Convert.ToInt32(stream.Length);
            metaDataReq.Removable            = Settings.Removable;
            metaDataReq.OneShot              = Settings.OneShot;
            if (Settings.TTLUnit != 3) // everything except the expire time -1
            {
                metaDataReq.Ttl = Convert.ToInt32(GetMultiplyIndex(2, Settings.TTLUnit) * Settings.TTL * 60);
            }
            else
            {
                metaDataReq.Ttl = -1;
            }
            if (Settings.HasComment)
            {
                metaDataReq.Comment = Settings.Comment;
            }
            if (Settings.IsSecured)
            {
                metaDataReq.Login    = Settings.Login;
                metaDataReq.Password = Settings.Password;
            }
            string metaDataResp             = SendRequest(HttpMethod.POST, Settings.URL + "/upload", JsonConvert.SerializeObject(metaDataReq), headers: requestHeaders);
            UploadMetadataResponse metaData = JsonConvert.DeserializeObject <UploadMetadataResponse>(metaDataResp);

            requestHeaders["x-uploadtoken"] = metaData.uploadToken;
            string       url        = $"{Settings.URL}/file/{metaData.id}/{metaData.files.First().Value.id.ToString()}/{fileName}";
            UploadResult FileDatReq = SendRequestFile(url, stream, fileName, "file", headers: requestHeaders);

            return(ConvertResult(metaData, FileDatReq));
        }