-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAssetModel.cs
More file actions
91 lines (79 loc) · 3.59 KB
/
Copy pathAssetModel.cs
File metadata and controls
91 lines (79 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.IO;
using System.Net.Http;
using Contentstack.Management.Core.Abstractions;
using Contentstack.Management.Core.Utils;
namespace Contentstack.Management.Core.Models
{
public class AssetModel: IUploadInterface
{
public string Title { get; set; }
public string Description { get; set; }
public string ParentUID { get; set; }
public string Tags { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
internal ByteArrayContent byteArray;
public AssetModel(string fileName, string filePath, string contentType, string title = null, string description = null, string parentUID = null, string tags = null):
this(fileName, File.OpenRead(filePath), contentType, title, description, parentUID, tags){ }
public AssetModel(string fileName, Stream stream, string contentType, string title = null, string description = null, string parentUID = null, string tags = null):
this(fileName, getBytes(stream), contentType, title, description, parentUID, tags){ }
public AssetModel(string fileName, byte[] bytes, string contentType, string title = null, string description = null, string parentUID = null, string tags = null) :
this(fileName, getByteArray(bytes), contentType, title, description, parentUID, tags){ }
public AssetModel(string fileName, ByteArrayContent byteArray, string contentType, string title = null, string description = null, string parentUID = null, string tags = null)
{
if (fileName == null)
{
throw new ArgumentNullException("fileName", CSConstants.FileNameRequired);
}
if (byteArray == null)
{
throw new ArgumentNullException("byteArray", CSConstants.UploadContentRequired);
}
FileName = fileName;
Title = title;
Description = description;
ParentUID = parentUID;
Tags = tags;
ContentType = contentType;
this.byteArray = byteArray;
this.byteArray.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType);
}
static private byte[] getBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
}
static private ByteArrayContent getByteArray(byte[] bytes)
{
return new ByteArrayContent(bytes);
}
public HttpContent GetHttpContent()
{
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(byteArray, "asset[upload]", FileName);
if (Title != null)
{
content.Add(new StringContent(Title), "asset[title]");
}
if (Description != null)
{
content.Add(new StringContent(Description), "asset[description]");
}
if (ParentUID != null)
{
StringContent contenst = new StringContent(ParentUID);
//contenst.Headers.Remove("Content-Type");
//contenst.Headers.ContentType.CharSet = string.Empty;
//contenst.Headers.Add("Content-Type", content.Headers.ContentType.ToString());
content.Add(contenst, "asset[parent_uid]");
}
if (Tags != null)
{
content.Add(new StringContent(Tags), "asset[tags]");
}
return content;
}
}
}