Skip to content

Commit 516595b

Browse files
committed
Fix downloading for not uploaded object (404 error)
1 parent f46cda4 commit 516595b

File tree

7 files changed

+83
-17
lines changed

7 files changed

+83
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
build
1717

1818
# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
19-
hs_err_pid*
19+
hs_err_pid*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ You can download latest stable version from [Maven Central](http://mvnrepository
3838
Version 0.4.0
3939

4040
* Fix url concatenation
41+
* Fix downloading for not uploaded object (404 error)
4142

4243
Version 0.3.0
4344

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/Client.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public BatchRes postBatch(@NotNull final BatchReq batchReq) throws IOException {
129129
return doWork(new Work<BatchRes>() {
130130
@Override
131131
public BatchRes exec(@NotNull Link auth) throws IOException {
132-
return doRequest(auth, new JsonPost<>(batchReq, BatchRes.class), URI.create(auth.getHref() + PATH_BATCH));
132+
return doRequest(auth, new JsonPost<>(batchReq, BatchRes.class), AuthHelper.join(auth.getHref(), PATH_BATCH));
133133
}
134134
}, batchReq.getOperation());
135135
}
@@ -147,7 +147,11 @@ public InputStream getObject(@NotNull final String hash) throws IOException {
147147
return doWork(new Work<InputStream>() {
148148
@Override
149149
public InputStream exec(@NotNull Link auth) throws IOException {
150-
return getObject(doRequest(auth, new MetaGet(), AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/" + hash)));
150+
final ObjectRes links = doRequest(auth, new MetaGet(), AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/" + hash));
151+
if (links == null) {
152+
throw new FileNotFoundException();
153+
}
154+
return getObject(links);
151155
}
152156
}, Operation.Download);
153157
}
@@ -177,6 +181,17 @@ public InputStream getObject(@NotNull final Links links) throws IOException {
177181
* @throws IOException On some errors.
178182
*/
179183
public boolean putObject(@NotNull final StreamProvider streamProvider) throws IOException {
184+
return putObject(streamProvider, generateMeta(streamProvider));
185+
}
186+
187+
/**
188+
* Generate object metadata.
189+
*
190+
* @param streamProvider Object stream provider.
191+
* @return Return object metadata.
192+
* @throws IOException On some errors.
193+
*/
194+
public Meta generateMeta(@NotNull final StreamProvider streamProvider) throws IOException {
180195
final MessageDigest digest = sha256();
181196
final byte[] buffer = new byte[0x10000];
182197
long size = 0;
@@ -188,8 +203,7 @@ public boolean putObject(@NotNull final StreamProvider streamProvider) throws IO
188203
size += read;
189204
}
190205
}
191-
final String hash = new String(Hex.encodeHex(digest.digest()));
192-
return putObject(streamProvider, hash, size);
206+
return new Meta(new String(Hex.encodeHex(digest.digest())), size);
193207
}
194208

195209
/**

gitlfs-client/src/test/java/ru/bozaro/gitlfs/client/ClientLegacyTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ru.bozaro.gitlfs.client.exceptions.ForbiddenException;
77
import ru.bozaro.gitlfs.client.io.StringStreamProvider;
88

9+
import java.io.FileNotFoundException;
910
import java.io.IOException;
1011
import java.nio.charset.StandardCharsets;
1112

@@ -16,7 +17,7 @@
1617
*/
1718
public class ClientLegacyTest {
1819
/**
19-
* Simple download.
20+
* Simple upload.
2021
*/
2122
@Test
2223
public void legacyUpload01() throws IOException {
@@ -59,6 +60,9 @@ public void legacyUpload04() throws IOException {
5960
replay.close();
6061
}
6162

63+
/**
64+
* Simple download
65+
*/
6266
@Test
6367
public void legacyDownload01() throws IOException {
6468
final HttpReplay replay = YamlHelper.createReplay("/ru/bozaro/gitlfs/client/legacy-download-01.yml");
@@ -67,4 +71,19 @@ public void legacyDownload01() throws IOException {
6771
Assert.assertEquals(new String(data, StandardCharsets.UTF_8), "Fri Oct 02 21:07:33 MSK 2015");
6872
replay.close();
6973
}
74+
75+
/**
76+
* Download not uploaded object
77+
*/
78+
@Test
79+
public void legacyDownload02() throws IOException {
80+
final HttpReplay replay = YamlHelper.createReplay("/ru/bozaro/gitlfs/client/legacy-download-02.yml");
81+
final Client client = new Client(new FakeAuthProvider(), replay);
82+
try {
83+
client.getObject("01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b");
84+
Assert.fail();
85+
} catch (FileNotFoundException ignored) {
86+
}
87+
replay.close();
88+
}
7089
}

gitlfs-client/src/test/java/ru/bozaro/gitlfs/client/HttpRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Response(@NotNull HttpMethod method) throws IOException {
7777
this.statusCode = method.getStatusCode();
7878
this.statusText = method.getStatusText();
7979
this.headers = new TreeMap<>();
80-
for (Header header : method.getRequestHeaders()) {
80+
for (Header header : method.getResponseHeaders()) {
8181
headers.put(header.getName(), header.getValue());
8282
}
8383
this.body = method.getResponseBody();

gitlfs-client/src/test/java/ru/bozaro/gitlfs/client/Recorder.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
import org.yaml.snakeyaml.Yaml;
66
import ru.bozaro.gitlfs.client.auth.AuthProvider;
77
import ru.bozaro.gitlfs.client.internal.HttpClientExecutor;
8-
import ru.bozaro.gitlfs.client.io.ByteArrayStreamProvider;
98

10-
import java.io.FileOutputStream;
11-
import java.io.IOException;
12-
import java.io.OutputStream;
13-
import java.io.OutputStreamWriter;
9+
import java.io.*;
1410
import java.nio.charset.StandardCharsets;
1511

1612
/**
@@ -25,15 +21,19 @@ public static void main(@NotNull String[] args) throws IOException {
2521

2622
doWork(new Client(auth, recorder));
2723

28-
recorder.getRecords();
29-
30-
Yaml yaml = YamlHelper.get();
31-
try (OutputStream replay = new FileOutputStream("replay.yml")) {
24+
final Yaml yaml = YamlHelper.get();
25+
final File file = new File("build/replay.yml");
26+
//noinspection ResultOfMethodCallIgnored
27+
file.getParentFile().mkdirs();
28+
try (OutputStream replay = new FileOutputStream(file)) {
3229
yaml.dumpAll(recorder.getRecords().iterator(), new OutputStreamWriter(replay, StandardCharsets.UTF_8));
3330
}
3431
}
3532

3633
private static void doWork(@NotNull Client client) throws IOException {
37-
client.putObject(new ByteArrayStreamProvider("Hello, world!!!".getBytes()));
34+
try {
35+
client.getObject("01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b");
36+
} catch (IOException e) {
37+
}
3838
}
3939
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
!!ru.bozaro.gitlfs.client.HttpRecord
2+
request:
3+
body: null
4+
headers:
5+
Accept: application/vnd.git-lfs+json
6+
Authorization: RemoteAuth Token-1
7+
href: http://gitlfs.local/test.git/info/lfs/objects/01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
8+
method: GET
9+
response:
10+
body: !text |-
11+
{"message":"Not Found","documentation_url":"https://github.com/contact"}
12+
headers:
13+
Access-Control-Allow-Credentials: 'true'
14+
Access-Control-Allow-Origin: '*'
15+
Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
16+
Content-Length: '72'
17+
Content-Security-Policy: default-src 'none'
18+
Content-Type: application/json; charset=utf-8
19+
Date: Wed, 07 Oct 2015 04:06:04 GMT
20+
Server: GitHub.com
21+
Status: 404 Not Found
22+
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
23+
X-Content-Type-Options: nosniff
24+
X-Frame-Options: deny
25+
X-GitHub-Media-Type: unknown
26+
X-GitHub-Request-Id: 4FA5AFEC:17F56:60D2406:56149A2C
27+
X-RateLimit-Limit: '3000'
28+
X-RateLimit-Remaining: '2999'
29+
X-RateLimit-Reset: '1444190824'
30+
X-XSS-Protection: 1; mode=block
31+
statusCode: 404
32+
statusText: Not Found

0 commit comments

Comments
 (0)